Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 How to write a pivot query for this.

Author  Topic 

satish.gorijala
Posting Yak Master

182 Posts

Posted - 2009-02-17 : 01:33:11
Hi, I am not getting how to write pivot query....
My requirement is as follows

Select * from table where ID = '555'

ID Col1 Col2 Col3
555 123 456 67

I want result as follows

ID 555
Col1 123
Col2 456
Col3 67

G. Satish

Nageswar9
Aged Yak Warrior

600 Posts

Posted - 2009-02-17 : 01:40:18
declare @temp table ( ID INT,Col1 int,Col2 int, Col3 int)
insert into @temp
select 555, 123, 456, 67

select string,value
from (
select * from @temp) p
unpivot
( value for string in (id,col1,col2,col3) ) as p2
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-02-17 : 03:03:31
or just use

SELECT ID,Col
FROM
(
SELECT ID, Col1 AS Col
FROM Table
WHERE ID=555
UNION ALL
SELECT ID, Col2
FROM Table
WHERE ID=555
UNION ALL
SELECT ID, Col3
FROM Table
WHERE ID=555
)t
Go to Top of Page
   

- Advertisement -