I need to pivot out some denormalized data but it repeats so I need it to pivot out the columns and then return multiple rows.
I have a table like this
INSERT #TheTable
VALUES
('StockCode' ,'a'),
('Warehouse' ,'b'),
('TrnYear' ,'c'),
('TrnMonth' ,'d'),
('EntryDate' ,'e'),
('TrnTime' ,'f'),
('StockCode' ,'1'),
('Warehouse' ,'2'),
('TrnYear' ,'3'),
('TrnMonth' ,'4'),
('EntryDate' ,'5'),
('TrnTime' ,'6')
But when I pivot it only returns one row:
SELECT StockCode,
Warehouse,
TrnYear,
TrnMonth,
TrnTime,
EntryDate
FROM #TheTable AS src
PIVOT (MAX(column_value)
FOR COLUMN_NAME in ([TrnYear], [TrnMonth], [EntryDate], [TrnTime], [StockCode], [Warehouse])) AS piv
Result:
StockCode Warehouse TrnYear TrnMonth TrnTime EntryDate
-------------------------------------------------------------
a b c d f e
But I need it to return
StockCode Warehouse TrnYear TrnMonth TrnTime EntryDate
-------------------------------------------------------------
a b c d f e
1 2 3 4 5 6