今天在统计报表的时候有这么一个需求,将一列字符串拼接成一行,然后展示到新的列中。
每一项的服务列表如下:
最终想要的结果是
sql如下:
select AuxTypeName + ',' from
(
SELECT DISTINCT(AuxTypeName) FROM _VIPHall_Link_AuxType
LEFT JOIN dbo.[_VIPHall] ON [_VIPHall_Link_AuxType].VIPHallID = [_VIPHall].VIPHallID
LEFT JOIN dbo._Dim_VIPHall_AuxType ON [_Dim_VIPHall_AuxType].AuxTypeID = [_VIPHall_Link_AuxType].AuxTypeID
WHERE [_VIPHall].VIPHallID = 157
) AS tmp
for xml path('')
整理出来也就是
select( select CAST(字段 as varchar)+','
from 表 for xml path('') ) as A
搞定!