June 18, 2013 at 2:11 am
Create Table Table1
(
Sno int,
Sname varchar(20)
);
insert into table1 values(1,'a'),
(2,'b'),
(1,'c'),
(2,'d')
select * from Table1
Sno Sname
-- ------
1 a
2 b
1 c
2 d
i want the output as following by using the sub-queries?
Sno Name1 Name2
1 a c
2 b d
June 18, 2013 at 2:39 am
This should give you some idea
SELECTSno,
MAX( CASE WHEN RN = 1 THEN Sname ELSE NULL END ) AS Name1,
MAX( CASE WHEN RN = 2 THEN Sname ELSE NULL END ) AS Name2
FROM(
SELECTROW_NUMBER() OVER ( PARTITION BY Sno ORDER BY Sname ) AS RN, *
FROMTable1
) AS T
GROUP BY Sno
How to post data/code on a forum to get the best help - Jeff Moden
http://www.sqlservercentral.com/articles/Best+Practices/61537/
June 18, 2013 at 2:45 am
Thanks For The Quick Reply KingSton
Viewing 3 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply