One way is to create a function that builds the comma seperated string and call it once for each region:set nocount on--==========================================--DDL/DML--tablecreate table myTable (regionid int ,typeid int)go--table datainsert myTableselect 1,1 union allselect 1,2 union allselect 1,3 union allselect 2,1 union allselect 2,2go--function to build comma seperated string of typeidscreate function dbo.fnBuildCSVTypeIDs (@regionid int)returns varchar(50)asbegin declare @str varchar(50) select @str = coalesce(@str + ', ' + convert(varchar,typeid), convert(varchar,typeid)) from myTable where regionid = @regionid order by typeid return @strendgo--==========================================--select statement using the functionselect regionid ,typeIDs = dbo.fnBuildCSVTypeIDs(regionid)from myTablegroup by regionid--==========================================--cleanupgodrop function dbo.fnBuildCSVTypeIDsdrop table myTablego
Be One with the OptimizerTG