2012-01-24
I have used the Coallace statement multiple times in SQL to rollup several records into a single Variable. This variable eventually gets utilized in a PivotTable Crosstabe query.
Well this time: I had new need.
Rollup several record ID numbers into a singled field whcih saparates the Id with a comma (delimeter).
Very easy to do; If you use other tools SQL 2008 R2 provides.
This is exactly What I needed:
===================================================
select distinct
Courseid
,relationalRecords = (select cast(AdvertiseId as varchar)+',' from
CourseAdvertising where CourseAdvertising.Courseid=Courses.Courseid for XML path(''))
from Courses
REFERENCE:
rollup table COALESCE sql statement single comma delimited with multiple records
sql select statement relational records comma delimited
with out using a cursor or temporary table or with out using SET STATEMENTS or VARIABLES
I originally found the how to here: