Framing a query | SQL Server Performance Forums

SQL Server Performance Forum – Threads Archive

Framing a query

I need a Set of Records in a tabular format like SELECT col1,col2,col3 FROM table1 col1 col2 col3
1 2 3 The Result should display
col1 1
col2 2
col3 3
How should the query be framed ?

See "cross-tab reports" in BOL Roji. P. Thomas
http://toponewithties.blogspot.com

quote:Originally posted by abha_dixit I need a Set of Records in a tabular format like SELECT col1,col2,col3 FROM table1 col1 col2 col3
1 2 3 The Result should display
col1 1
col2 2
col3 3
How should the query be framed ?

Try like this, but you need to know how PIVOT works first to understand this clearly. DECLARE @UnPivotTable TABLE
(ID int
, Col1 varchar(20)
, Col2 varchar(20)
, Col3 varchar(20)
) INSERT INTO @UnPivotTable VALUES (1, 101, 102, 103)
INSERT INTO @UnPivotTable VALUES (2, 201, 202, 203)
INSERT INTO @UnPivotTable VALUES (3, 301, 302, 303) –|Unpivot the table.
SELECT ID, [Column], Numbers
FROM
( SELECT ID, Col1, Col2, Col3
FROM @UnPivotTable ) p
UNPIVOT
(Numbers FOR [Column] IN
(Col1, Col2, Col3)
)AS unpvt
Thanks,
DilliGrg
]]>