日期:2014-05-18 浏览次数:20750 次
if object_id('[tb]') is not null drop table [tb] go create table [tb]([Name] varchar(1),[Age] int,[NO] int,[Remark] varchar(3)) insert [tb] select 'A',11,11,'ABC' union all select 'A',11,11,'CCC' union all select 'A',11,11,'AAA' union all select 'B',11,11,'CCC' union all select 'B',11,11,'DDD' union all select 'A',11,11,NULL go select name,age,no, remark=stuff((select ','+remark from tb where remark is not null and name=t.name and age=t.age and no=t.no for xml path('')),1,1,'') from tb t where remark is not null group by name,age,no UNION ALL SELECT * FROM TB WHERE REMARK IS NULL /** name age no remark ---- ----------- ----------- ----------------------- A 11 11 ABC,CCC,AAA B 11 11 CCC,DDD A 11 11 NULL (3 行受影响) **/