日期:2014-05-17 浏览次数:20410 次
;with c1(id, 销售企业, 购买企业, 购买量) -- c1准备数据
as
(
select 1, 'a', 'x', 10 union all
select 2, 'a', 'y', 20 union all
select 3, 'b', 'z', 30
)
,c2 as
(
SELECT 1 groupId,
'销售企业' + 销售企业 + ',' f1,
'购买企业' + 购买企业 f2,
'购买量' + CAST(购买量 AS VARCHAR(10)) f3
from c1
)
SELECT DISTINCT
groupid,
STUFF((select ';'+(f1 + f2 + f3) from c2 where groupid = d.groupId FOR XML PATH('')),1,1,'') result
from c2 d
groupid result
----------- -----------------------------------------------
1 销售企业a,购买企业x购买量10;销售企业a,购买企业y购买量20;销售企业b,购买企业z购买量30
(1 行受影响)