Pages

Monday, April 25, 2011

How to Use Pivot table concept in SQL.

Unit_id Unit_price Abbr
1 250 S
1 300 A
1 350 T

DROP TABLE TestTable
GO
CREATE TABLE TestTable
(
ID INT IDENTITY(1,1) PRIMARY KEY,
UNIT INT,
Price INT,
Abbr VARCHAR(1),
)
GO
INSERT TestTable
SELECT 1,250,'s' UNION
SELECT 1,300,'a' UNION
SELECT 1,350,'t'
GO
SELECT * FROM TestTable
GO


Display Result as:

Unit_id  A T
1 250 300 350