Sunday, 15 April 2012

sql - SELECT records as comma separated string -



sql - SELECT records as comma separated string -

this question has reply here:

simulating group_concat mysql function in sql server? 8 answers

so have next tables:

table 1 fname mi lname empid john smith 1202 bob doe 9815 table 2 unid empid 1015 1202 1015 9815 table 3 unid item 1015 abc 1015 def

my intended output should (when supplying unid=1015)

fname mi lname item john smith abc, def bob doe

now ideal, i'm more happy deal repeated [item] values on front end end.

my current statement is:

select p.fname,p.mi,p.lname, ac.equip table1 t1, table2 t2, table3 t3 t1.empid = t2.empid , t2.unid = t3.unid , t2.unid = '1015' grouping t1.fname, t1.mi, t1.lname,t3.equip

for life of me, cannot figure out how values in item (which can 0 or more maximum of 8) 1 comma separated string. problem is, due site/client constraints, cannot utilize sp has done in 1 sql statement.

this on sql server 2008 r2.

select distinct t.fname,t.mi,t.lname, stuff((select distinct i.item + ',' table3 t.unid = tt.unid , i.unid = '1015' order i.unid xml path(''),type).value('.', 'nvarchar(max)') , 1, 0, ' ') table1 t inner bring together table2 tt on tt.empid = t.empid grouping t.fname, t.mi, t.lname

sql sql-server tsql select sql-server-2008-r2

No comments:

Post a Comment