标签:通过 max 等于 字符串 nbsp rtb end val col
前提:sqlserver 2005及以上版本
表(tb)
id value
1 aa
2 cc
3 bb
3 dd
4 aa
4 cc
4 dd
5 aa
优化前:
select id, value = (select value+‘,‘ from tb as rtb where rtb.id = a.tb.id for xml path(‘‘))
from tb as a
group by a.id
优化后:(思路:将count(0)数等于1或者等于2的记录,通过min,max处理,减少每个id都要查询表)
select id, value =case count(0) when 1 then min(value)
when 2 then min(value)+‘,‘+max(value)
else (select value+‘,‘ from tb as rtb where rtb.id = a.tb.id for xml path(‘‘)) end
from tb as a
group by a.id
sqlserver 2005及以上版本 group by 如何合并字符串优化记?
标签:通过 max 等于 字符串 nbsp rtb end val col
原文地址:https://www.cnblogs.com/mirsh/p/11670627.html