读书人

SQL求高手!解决思路

发布时间: 2012-01-30 21:15:58 作者: rapoo

SQL求高手!!!!!!!!!!!!!!!

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;

读书人网 >SQL Server

热点推荐