日期:2014-05-17  浏览次数:20501 次

这种效果如何统计??

table1  
字段ID     字段2          字段3         字段4
101        2012-01-01     2012-01-01    ADBDD
102
...
table2
自增ID     字段ID          字段23
1             101          A部门
2             101          B部门
3             101          C部门
4             102          ..
table3
自增ID     字段ID          字段33
1             101          张三
2             101          李四
3             102          ..
table4
自增ID     字段ID          字段43
1             101          C部门
2             101          B部门
3             102          ..
table5
自增ID     字段ID          字段53
1             101          打牌
2             101          麻将
3             102          ..

统计成
字段ID       字段2       字段3          字段4   字段23               字段33      字段43         字段53
101        2012-01-01   2012-01-01    ADBDD   A部门,B部门,C部门   张三,李四    C部门,B部门    打牌,麻将

------最佳解决方案--------------------
select 字段ID,字段2,字段3,字段4,stuff((select ','+字段23 from table2 where table2.字段ID=tb1.字段ID for xml path('')),1,1,'') 字段23 ,stuff((select ','+字段33 from table3 where table3.字段ID=tb1.字段ID for xml path('')),1,1,'') 字段33,stuff((select ','+字段43 from table4 where table4.字段ID=tb1.字段ID for xml path('')),1,1,'') 字