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
 General SQL Server Forums
 New to SQL Server Programming
 How to take values in a Row

Author  Topic 

arorarahul.0688
Posting Yak Master

125 Posts

Posted - 2008-11-27 : 01:44:22
hi i have a following pattern as input

Col1 Col2
A Y
B Y
C Y
D N

I need Output as

A,B,C Y
D N


Hope its not tough enough for you guys so plz help me in this. Thanks For the efforts.

Rahul Arora
07 Batch
NCCE Israna,


######################
IMPOSSIBLE = I+M+POSSIBLE

snSQL
Master Smack Fu Yak Hacker

1837 Posts

Posted - 2008-11-27 : 01:51:44
This is a common question, do some searching for concatenation, here's one thread that discusses it
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=53293
Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2008-11-27 : 02:13:31
DECLARE @table table (Col1 char(2), Col2 char(2))
INSERT INTO @table select 'A', 'Y' UNION ALL
SELECT 'B', 'Y' UNION ALL
select 'C', 'Y' UNION ALL
SELECT 'D', 'N'

SELECT DISTINCT STUFF((SELECT DISTINCT ',' + CAST(col1 AS VARCHAR(255))
FROM @table WHERE col2 = t.col2
FOR XML PATH('')), 1, 1, ''), col2 FROM @table t
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-11-27 : 02:26:56
quote:
Originally posted by bklr

DECLARE @table table (Col1 char(2), Col2 char(2))
INSERT INTO @table select 'A', 'Y' UNION ALL
SELECT 'B', 'Y' UNION ALL
select 'C', 'Y' UNION ALL
SELECT 'D', 'N'

SELECT DISTINCT STUFF((SELECT DISTINCT ',' + CAST(col1 AS VARCHAR(255))
FROM @table WHERE col2 = t.col2
FOR XML PATH('')), 1, 1, ''), col2 FROM @table t


This will work from SQL Server 2005 version onwards

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2008-11-27 : 03:14:55
quote:
Originally posted by arorarahul.0688

hi i have a following pattern as input

Col1 Col2
A Y
B Y
C Y
D N

I need Output as

A,B,C Y
D N


Hope its not tough enough for you guys so plz help me in this. Thanks For the efforts.

Rahul Arora
07 Batch
NCCE Israna,


######################
IMPOSSIBLE = I+M+POSSIBLE




See once output

the output is same
Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2008-11-27 : 03:16:06
quote:
Originally posted by bklr

[quote]Originally posted by arorarahul.0688

hi i have a following pattern as input

Col1 Col2
A Y
B Y
C Y
D N

I need Output as

A,B,C Y
D N


Hope its not tough enough for you guys so plz help me in this. Thanks For the efforts.

Rahul Arora
07 Batch
NCCE Israna,


######################
IMPOSSIBLE = I+M+POSSIBLE




See once output

the output is same for this query
SELECT DISTINCT STUFF((SELECT DISTINCT ',' + CAST(col1 AS VARCHAR(255))
FROM @table WHERE col2 = t.col2
FOR XML PATH('')), 1, 1, '')as col1, col2 FROM @table t
Go to Top of Page

Jai Krishna
Constraint Violating Yak Guru

333 Posts

Posted - 2008-11-27 : 03:29:58
SELECT DISTINCT
STUFF((SELECT ','+ col1 FROM #table WHERE col2=t.col2 FOR XML PATH('')),1,1,''),
t.col2
from #table t

Jai Krishna
Go to Top of Page

Nageswar9
Aged Yak Warrior

600 Posts

Posted - 2008-11-27 : 03:37:02
select distinct stuff((select distinct ','+ col1 from ##table t where t.col2=t1.col2 for xml path('')),1,1,'')+' '+t1.col2 from ##table t1


I Struggle For Excellence
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-11-27 : 03:40:12
will now each of people who see this change variable names and post as new suggestion?
Go to Top of Page
   

- Advertisement -