- SQL code
这样的数据结构ID COL1 COL2 COL31 1 2 32 1 2 33 1 1 2 要显示的结果ID COL1 COL2 COL31,2 1 2 33 1 1 2这个SQL要怎么写啊
------解决方案--------------------
- SQL code
select id=stuff((select ','+rtrim(id) from tb where col2=a.col2 and col3=a.col3 and col4=a.col4 from xml path('')),1,1,''), col2,col3,col4from tb group by col2,col3,col4
------解决方案--------------------
select id=stuff((select ','+CONVERT(varchar,id) from tb where tb.col1=a.col1 and col2=a.col2
and col3=a.col3 for xml path('')),1,1,'') ,col1,col2,col3 from tb a group by col1,col2,col3;