sql中怎样把同一张表中相同字段的内容合并为一条记录?并统计数量

2024-10-30 07:17:53
推荐回答(3个)
回答1:

12SELECT Customer_ID,Customer_Name,

class="baidu-highlight">STUFF((SELECT ','+Linkman_Name FROM Linkman WHERE Customer_ID=Customer.Customer_ID FOR XML

"https://www.baidu.com/s?wd=PATH&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1Y3ujRkn1R1uW9hryN-

njF90ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EnHm1rH04rH6snjnkn1mYnjR3n0" target="_blank" 

class="baidu-highlight">PATH('')),1,1,'') Linkman_Name  FROM Customer

扩展资料;

把表名tt换下就可以了购买产品id是字符串类型的话用这个select a.客户id,stuff((select ','+购买产品id from tt b  wherea.客户id=b.客户id  for XML path('') ),1,1,'') gg from tt a  group by a.客户id

如果购买产品id是整型的话,用这个select a.客户id,stuff((select ','+cast(购买产品id as nvarchar(max)) from tt b  wherea.客户id=b.客户id  for XML path('') ),1,1,'') gg from tt a  group by a.客户id

参考资料;百度百科-sql

回答2:

--不用那么麻烦 用这个 把表名tt换下就可以了
--你的购买产品id是字符串类型的话用这个
select a.客户id,stuff((select ','+购买产品id from tt b where
a.客户id=b.客户id for XML path('') ),1,1,'') gg from tt a
group by a.客户id
--如果购买产品id是整型的话,用这个
select a.客户id,stuff((select ','+cast(购买产品id as nvarchar(max)) from tt b where
a.客户id=b.客户id for XML path('') ),1,1,'') gg from tt a
group by a.客户id

回答3:

这可能要用存储过程,先创建两个表,一个ID,一个你要用的结果表,然后对ID表用游标,将购买ID的列加出来,然后插入到你的结果集