ttajic
2/7/2020 - 11:05 AM

Rows to column/list of concatated strings of subquery rows

select [E-Mail], COUNT(*),
stuff((SELECT ( ', ' + No_ )
                       FROM SPICA_NAV_110_PROD.dbo.[Špica SLO$Contact] t2
                      WHERE t2.Type=1 AND c.[E-Mail] = t2.[E-Mail]
                      ORDER BY [No_]
                        FOR XML PATH( '' )
                    ), 1, 1, '' )

FROM SPICA_NAV_110_PROD.dbo.[Špica SLO$Contact] as c 
WHERE  c.Type=1
GROUP BY [E-Mail] 
HAVING COUNT(*)>1

/* RESULT

E-Mail                                  Count     No_ list
vrtec-mavrica.brezice@guest.arnes.si	  3	        52844, 67838, 67839
lee.hannis@hrsltd.com	                  2	        27565, 71775
andrej.ule@autodelta.si	                2	        58914, 59184

*/