表中数据如下
Table1
type col1
A    A1
A    A2
B    B1
B    B2分组需要显示后的结果为
type col1
A    A1;A2
B    B1;B2
请问这个种形式如何实现?谢谢~

解决方案 »

  1.   


    with Table1(type,col1)as(
    select 'A','A1' union all
    select 'A','A2' union all
    select 'B','B1' union all
    select 'B','B2')
    select distinct type,
    col1=stuff((select ';'+col1 from table1 where a.type=type for xml path('')),1,1,'') from table1 a
      

  2.   

    修改了一下,貌似这个执行效率要高些SELECT B.RepairListID,LEFT(ProList,LEN(ProList)-1) as list FROM (
    SELECT RepairListID,
    (SELECT ProductID+';' FROM #temp 
      WHERE RepairListID=A.RepairListID 
      FOR XML PATH('')) AS ProList
    FROM #temp a
    GROUP BY RepairListID
    ) B