开发者

Aggregate/concatenate values from a column into a single row

I have a couple thousand franchises, and about 3 or 4 service groups. The service groups are in a separate table, then i have third table joining them together. A franchise may be connected to 1 or many service groups. What I am trying to achieve is listing out the franchises, with a column labeled "Service Categories". It would then be a comma separated list of which services they provide. So right now, here's my output:

id  Service Groups     name                    email                address      City         State
1   Cleaning Services  Franchise of LocationX  example@example.com  123 Fake st. Springfield  TheOneTheSimpsonsLiveIn
2   Disaster Services  Franchise of LocationX  example@example.com  123 Fake st. Springfield  TheOneTheSimpsonsLiveIn

I would like to have it so it becomes this:

id  Service Groups                        name                    email                address      City         State
1   Cleaning Services, Disaster Services  Franchise of LocationX  example@example.com  123 Fake st. Springfield  TheOneTheSimpsonsLiveIn

Any suggestions or references of methods/functions I can use to 开发者_高级运维do this would be greately appreciated. Thanks!


try this

Declare @TempServiceGroups varchar(max)=''
Select COALESCE(@TempServiceGroups,'')+ServiceGroups+',' from MyTable
Select @TempServiceGroups AS ServiceGroups , Name,Email,Address,City,State
from MyTable
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜