Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6575305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:22:14+00:00 2026-05-25T15:22:14+00:00

I am trying to create a query to combine the following information: FileID ErrorCode

  • 0

I am trying to create a query to combine the following information:

FileID  ErrorCode  ErrorDesc             ErrorCount
  1         4      Bad File Name             3
  2         6      Bad File Code            56
  3         4      Bad File Name             2
  3        12      Line Length Invalid       3
  3        17      Missing Req Fields      150

I want to combine all rows based on the FileID so that all information for a given FileID would appear on the same line as the following:

1  4  Bad File Name     3
2  6  Bad File Code    56
3  4  Bad File Name     2     12  Line Length Invalid  3     17 Missing Req Fields  150

The problem I am running it to there will be an unknown amount of errors per given file. It could have 1-50 errors and I would like to combine all of that information under one row. I am not sure if this is possible or if there is another way to look at this issue. My end goal is to end up creating a report off of this data. Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T15:22:14+00:00Added an answer on May 25, 2026 at 3:22 pm

    A little more involved than Mikael’s – the main difference is that columns are maintained here (however the columns for each error code other than the first “line up”).

    Setup:

    CREATE TABLE dbo.t
    (
        FileID INT,
        ErrorCode INT,
        ErrorDesc VARCHAR(255),
        ErrorCount INT
    );
    
    INSERT dbo.t VALUES
    (1,4,'Bad File Name',3),
    (2,6,'Bad File Code',56),
    (3,4,'Bad File Name',2),
    (3,12,'Line Length Invalid',3),
    (3,17,'Missing Req Fields',150);
    

    Code:

    DECLARE 
        @sql0 NVARCHAR(MAX) = N'',
        @sql1 NVARCHAR(MAX) = N'',
        @sql2 NVARCHAR(MAX) = N'',
        @minC INT;
    
    SELECT @minC = MIN(ErrorCode) FROM dbo.t;
    
    SELECT @sql1 += REPLACE(',x$.ErrorCode AS Code$,
        x$.ErrorDesc AS Desc$,x$.ErrorCount AS Count$', 
        '$', CONVERT(VARCHAR(12), ErrorCode))
        FROM dbo.t WHERE ErrorCode > @minC GROUP BY ErrorCode ORDER BY ErrorCode;
    
    SELECT @sql2 += REPLACE(' 
        LEFT OUTER JOIN x AS x$ ON z.FileID = x$.FileID 
        AND x$.ErrorCode = $
        AND x$.ErrorCode > z.ErrorCode', '$', CONVERT(VARCHAR(12), ErrorCode))
        FROM dbo.t WHERE ErrorCode > @minC GROUP BY ErrorCode ORDER BY ErrorCode;
    
    SET @sql0 = ';WITH y AS (
        SELECT FileID, ErrorCode, ErrorDesc, ErrorCount,
            rn = ROW_NUMBER() OVER (PARTITION BY FileID ORDER BY ErrorCode)
        FROM dbo.t
    ),
    z AS ( SELECT FileID, ErrorCode, ErrorDesc, ErrorCount FROM y WHERE rn = 1 ),
    x AS ( SELECT FileID, ErrorCode, ErrorDesc, ErrorCount FROM y WHERE rn > 1 )
    SELECT z.*' + @sql1 + ' FROM z
    ' + @sql2;
    
    -- PRINT @sql0; -- to see what has been crafted    
    EXEC sp_executesql @sql0;
    GO
    

    Clean-up:

    DROP TABLE dbo.t;
    GO
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a single query that will combine the following two queries.
I'm trying to create a SQL 2005 query to retrieve and combine records from
Trying to create linq query from following xml. <root> <mileage value=100> </mileage> <mileage value=75>
I'm trying to create a query with the Criteria API from JPA 2.0, but
I'm trying to create a simple database table using the PHP MySQL query Create
I have 4 databases with similar schema's, and I'm trying to create a query
I'm trying to create a linq query based on some dynamic/optional arguments passed into
I'm trying to create a faster query, right now i have large databases. My
I'm trying to create an SQL query in PHP to update a table. Is
I am trying to create a report in SSRS2000 that will query an ORACLE

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.