Sid
Sid

Reputation: 765

Embeded Select: Concatenate Query in rows

Can I somehow do this in mssql query alone, no SP or declares? Because I am going to embed this select in my Powerbuilder.

ID      text
1       a
1       b
1       c
2       a
2       d
3       e


into

ID      text
1       abc
2       ad
3       e

Upvotes: 0

Views: 258

Answers (2)

Dhinakar
Dhinakar

Reputation: 4151

Try like below Query

 SELECT
   ID, 
   text= 
   replace(
   STUFF((SELECT ','+text FROM test WHERE ID=A.ID FOR XML PATH('')) , 1 , 1 , '' )
   ,',','')
   FROM 
   test A group by ID

SQLFiddle Demo here

Upvotes: 0

MarcinJuraszek
MarcinJuraszek

Reputation: 125620

I'm not sure I understood your question, but check out this DEMO:

SELECT DISTINCT
   ID,
   (SELECT ''+text FROM Test WHERE ID=A.ID FOR XML PATH('')) AS text
FROM 
   Test A

Results:

ID  TEXT
1   abc
2   ad
3   e

Upvotes: 2

Related Questions