首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

SQL求高手!解决思路

2012-01-30 
SQL求高手!!!!!!!!!!!!!!!SQL code这样的数据结构IDCOL1COL2 COL3112321233112 要显示的结果IDCOL1COL2 CO

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;

热点排行