I have a scenario where I wish to display the column values(Val1) for each unique column value (Val2) as an individual column, with a max of 10 columns.
CREATE TABLE #TEMP1 (Val1 NVARCHAR(4), Val2 NVARCHAR(10));
insert into #Temp1 Values ('S01','00731')
insert into #Temp1 Values ('S02','00731')
insert into #Temp1 Values ('S03','00731')
insert into #Temp1 Values ('S04','00731')
insert into #Temp1 Values ('S05','00731')
insert into #Temp1 Values ('S06','00731')
insert into #Temp1 Values ('S07','00731')
insert into #Temp1 Values ('S08','00731')
insert into #Temp1 Values ('S09','00731')
insert into #Temp1 Values ('S07','00731')
insert into #Temp1 Values ('S04','00741')
insert into #Temp1 Values ('S01','00746')
insert into #Temp1 Values ('S01','00770')
insert into #Temp1 Values ('S01','00771')
insert into #Temp1 Values ('S02','00771')
Val1 Val2
--------------------------
S01 00731
S02 00731
S03 00731
S04 00731
S05 00731
S06 00731
S07 00731
S08 00731
S09 00731
S07 00731
S04 00741
S01 00746
S01 00770
S01 00771
S02 00771
I then use a pivot column to show each unique Val2 value and with a max of 10 Val1 values as columns.
SELECT [Val2],
c1, c2, c3, c4, c5, c6, c7, c8, c9, c10
FROM
(SELECT Val1, Val2
FROM #TEMP1) AS PivotTable
PIVOT
(
MAX([PivotTable].[Val1])
FOR
Val1
IN
(C1, c2, c3, c4, c5, c6, c7, c8, c9, c10)
) AS PivotTable;
I wish to have results like:
Val2 c1 c2 c3 c4 c5 c6 c7 c8 c9 c10
--------------------------------------------------------------------------------------
00731 S01 S02 S03 S04 S05 S06 S07 S08 S09 S07
00741 S04 NULL NULL NULL NULL NULL NULL NULL NULL NULL
00746 S01 NULL NULL NULL NULL NULL NULL NULL NULL NULL
00770 S01 NULL NULL NULL NULL NULL NULL NULL NULL NULL
00771 S01 S02 NULL NULL NULL NULL NULL NULL NULL NULL
But i actually just get all NULL values for the columns:
Val2 c1 c2 c3 c4 c5 c6 c7 c8 c9 c10
--------------------------------------------------------------------------------------
00731 NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL
00741 NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL
00746 NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL
00770 NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL
00771 NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL
Your requirements are not totally clear but it looks like you are trying to create a new column named
cwith then arow_number()associated with it —c1, c2 c3, etc.If you were to use the following in your subquery:
See SQL Fiddle with Demo
You would get the result:
Which seems to be the result that you then want to
PIVOT. You would then apply thePIVOTto this using:See SQL Fiddle with Demo. Your final result is then:
Note: my results are slightly different from what you are requesting as the desired result because I am performing an
ORDER BY val1which causes theS07values to be grouped together.There is no order of data in a database unless you request one, so there is no guarantee that one of the
S07values will appear asC10. You could use the following to get the result but there is no guarantee that the result will always be in the correct order:See SQL Fiddle with Demo. Using the
order by (select 1)alters the order of the data but it does not guarantee that it will always be in that order. The result is: