Monday, 15 July 2013

sql - Concatenate many rows into comma-separated list based on unique id -



sql - Concatenate many rows into comma-separated list based on unique id -

this question has reply here:

simulating group_concat mysql function in sql server? 8 answers

i'm new sql (running sql server 2012), query i'm running returning these results.

idnum email ---------------- 1 a@a.com 1 b@b.com 1 c@c.com 2 d@d.com 2 e@e.com 3 f@f.com 3 g@g.com 3 h@h.com 4 i@i.com 5 j@j.com 5 k@k.com

i next result set (a comma separated list unique each id)

idnum emails --------------------------------- 1 a@a.com,b@b.com,c@c.com 2 d@d.com,e@e.com 3 f@f.com,g@g.com,h@h.com 4 i@i.com 5 j@j.com,k@k.com

i've been trying follow of answers other questions not having luck. i'm sure it's combination of inexperience & other questions i'm finding results single table. query getting results multiple tables if makes difference, similar to

select distinct s.idnum, e.email pupil s bring together email e on e.guid = s.guid s.activeyear = 1 , e.activeemail = 1

can help? thanks.

******update******

i ended using next query after reading few more articles here , on website. hope helps in future.

use databasename select s.idnum, stuff(( select ',' + e.email [text()] email e e.guid = s.guid , e.activeemail = 1 xml path('') ), 1, 1, '' ) emails pupil s s.activeyear = 1

in oracle listagg function, sql-server seek question, there's few different options there:

listagg in sqlserver

sql tsql concatenation string-aggregation

No comments:

Post a Comment