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
 queery to show all data according to id

Author  Topic 

nextaxtion
Yak Posting Veteran

54 Posts

Posted - 2015-02-01 : 06:58:35
i have a table i need to show data according to id. distinct id and all name should be comma separated against each id.

Please suggest how to write effective query for this scenario.

DECLARE @TBL TABLE (ID NUMERIC(10), NAME VARCHAR(10))

INSERT INTO @TBL (ID,NAME)
VALUES(1, 'A'), (1,'B') ,(1,'C') ,(2,'E') ,(2,'B') , (3,'F') , (3,'G')

output :

id name

1 a,b,c
2 e,b
3 f,g

prithvi nath pandey

bitsmed
Aged Yak Warrior

545 Posts

Posted - 2015-02-01 : 13:29:13
[url]http://www.dbsimplified.com/2011/06/groupconcat-in-microsoft-sql-server.html[/url]
Go to Top of Page

akibintel
Starting Member

7 Posts

Posted - 2015-02-02 : 01:06:35
Hi heres ur actually query

SELECT distinct
id,
(
SELECT NAme+','
FROM mytbl t2
WHERE t2.id = t1.ID
FOR XML PATH('')
) Concatenated
FROM mytbl t1

Thanks & Regards
Go to Top of Page
   

- Advertisement -