Convert multiple rows into One row multiple columns based on the rownumber

I have a SQL table.

All I need to do is to convert rows into into one based on rownumber and column1.

My code is not working.

Here's my code:

SELECT [Filename]
      ,[ImportDate]
      ,[DataName] 
,MAX(CASE WHEN [RowNumber] = 1 THEN [Column1] ELSE NULL END) AS [1_COL1]
,MAX(CASE WHEN [RowNumber] = 2 THEN [Column1] ELSE NULL END) AS [2_COL1]
,MAX(CASE WHEN [RowNumber] = 1 THEN [Column2] ELSE NULL END) AS [1_COL2]
,MAX(CASE WHEN [RowNumber] = 2 THEN [Column2] ELSE NULL END) AS [2_COL2]
,MAX(CASE WHEN [RowNumber] = 1 THEN [Column3] ELSE NULL END) AS [1_COL3]

,MAX(CASE WHEN [RowNumber] = 1 THEN [Column4] ELSE NULL END) AS [1_COL4]
,MAX(CASE WHEN [RowNumber] = 1 THEN [Column5] ELSE NULL END) AS [1_COL5]
,MAX(CASE WHEN [RowNumber] = 2 THEN [Column3] ELSE NULL END) AS [2_COL3]
,MAX(CASE WHEN [RowNumber] = 2 THEN [Column4] ELSE NULL END) AS [2_COL4]

,MAX(CASE WHEN [RowNumber] = 2 THEN [Column5] ELSE NULL END) AS [2_COL5]





FROM (SELECT a.[Column0]
      ,a.[Column1]
      ,a.[Column2]
      ,a.[Column3]
      ,a.[Column4]
      ,a.[Column5]
      ,a.[Column6]
      ,a.[Column7]
      ,a.[Column8]
      ,a.[Column9]
      ,a.[Filename]
      ,a.[ImportDate]
      ,a.[DataName]
      ,a.[RowNumber] 
      FROM [dbo].[Test] a 
     ) crossTab 
GROUP BY [Filename]
      ,[ImportDate]
      ,[DataName]
	 

Open in new window


Please see attached.

Please help.

Thank you very much.
SQL-table-and-Output.xlsx
Queennie LAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mark WillsTopic AdvisorCommented:
That looks do-able. Think the MAX rownumber might need some more thought. Seems the same column gives results depending on rownumber = 1 or 2 - that might need to be resolved first (maybe).

Let me have a closer look at your attachments and I'll get back to you soon...
0
Mark WillsTopic AdvisorCommented:
think the group by was an appealing idea, but lead you down the wrong path. There isnt really anything to group by until you get the columns...

So, try to get the columns first, and then you identify that rownumber 2 is really an extension of rownumber 1  and that is the challenge.

Fortunately we have a uniqueID.... So lets use that in a join back on itself to get part 'b'
SELECT a.[Filename]
      ,a.[ImportDate]
      ,a.[DataName] 
      ,CASE WHEN a.[RowNumber] = 1 THEN a.[Column1] END AS [1_COL1]
      ,CASE WHEN b.[RowNumber] = 2 THEN b.[Column1] END AS [2_COL1]
      ,CASE WHEN a.[RowNumber] = 1 THEN a.[Column2] END AS [1_COL2]
      ,CASE WHEN b.[RowNumber] = 2 THEN b.[Column2] END AS [2_COL2]
      ,CASE WHEN a.[RowNumber] = 1 THEN a.[Column3] END AS [1_COL3]

      ,CASE WHEN a.[RowNumber] = 1 THEN a.[Column4] END AS [1_COL4]
      ,CASE WHEN a.[RowNumber] = 1 THEN a.[Column5] END AS [1_COL5]
      ,CASE WHEN b.[RowNumber] = 2 THEN b.[Column3] END AS [2_COL3]
      ,CASE WHEN b.[RowNumber] = 2 THEN b.[Column4] END AS [2_COL4]

      ,CASE WHEN b.[RowNumber] = 2 THEN b.[Column5] END AS [2_COL5]

FROM [dbo].[Test] a
INNER JOIN [dbo].[Test] b on a.uniqueID + 1 = b.uniqueID 
WHERE a.RowNumber = 1 and b.rownumber = 2

Open in new window

Obviously I havent added in the extra columns (column sequence seems different from what you wanted), but should give you the basis to add in the rest of them.

Does that make sense ?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Queennie LAuthor Commented:
@Mark Willis:

Yes it makes sense.

I will run this and test and I will let you know.

Thank and thank you again.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Queennie LAuthor Commented:
@Mark Willis:

Thank and Thank you again and again.

I have been working with this query for 3 days.

It is working.

I appreciate your help from the bottom of my heart.

Thank you.
0
Queennie LAuthor Commented:
@Mark Willis:

Thank and Thank you again and again.

I have been working with this query for 3 days.

It is working.

I appreciate your help from the bottom of my heart.

Thank you.
0
Mark WillsTopic AdvisorCommented:
It is my pleasure to have been able to help. Enjoyed the challenge.

Thank you very much for sharing your problem and allowing us to assist you in finding a solution.

Kind Regards,
Mark Wills
1
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
SQL

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.