Tuesday, February 25, 2014

Columns to rows

Try the below:



Create Table Test_Satish(Dataset varchar(10),Batch bigint, [Check] Varchar(100))
Insert into Test_Satish Values
('VRR',10000,'814028770003608'),
('VRX',28745,'814028770003608'),
('VRY',24512,'814028770003608'),
('VRZ',15114,'814028770003608'),
('VRR',2451,'814028770003608'),
('VRO',29101,'814028770003679'),
('VRP',26533,'814028770003679'),
('VRQ',26927,'814028770003679'),
('VRT',15114,'814028770003679')

SELECT [Check] AS Cost_Sorted_By_Production_Days,
[VRR],[VRX],[VRY],[VRZ],[VRO],[VRP],[VRQ],[VRT]
FROM
(SELECT Dataset, Batch,[Check]
FROM Test_Satish) AS SourceTable
PIVOT
(
MAX(Batch)
FOR Dataset IN ([VRR],[VRX],[VRY],[VRZ],[VRO],[VRP],[VRQ],[VRT])
) AS PivotTable;

Drop table Test_Satish


No comments:

Post a Comment