如何组合相同列记录的结果

时间:2015-02-03 06:49:12

标签: sql-server tsql

select PRODUCTPRICEPLANID,PRICEPLANDESCRIPTION,OPERATORNAME,SOCCODE from 
                (SELECT  
                ppp.PRODUCTPRICEPLANID,
                ps.PRICEPLANDESCRIPTION,
                O.OPERATORNAME,
                osoc.SOCCODE
FROM PRODUCTPRICEPLAN ppp 
join PRICEPLANSUMMARY ps 
on ppp.PRODUCTPRICEPLANID=ps.PRODUCTPRICEPLANID_FK 
join PRICEPLANSOC psoc 
on ppp.PRODUCTPRICEPLANID=psoc.ProductPricePlanID_FK
join OPERATORSOC osoc 
on psoc.OPERATORSOCID_FK=osoc.OPERATORSOCID
join operators o on osoc.OPERATORID_FK=O.OPERATORID
where ppp.PRODUCTPRICEPLANID=95) t 
GROUP BY  PRODUCTPRICEPLANID,PRICEPLANDESCRIPTION,OPERATORNAME,SOCCODE

目前我从查询中获取此结果集:

PRODUCTPRICEPLANID  PRICEPLANDESCRIPTION    OPERATORNAME    SOCCODE
 95                    TMUS UnBundled        T-Mobile US     BRVC
 95                    TMUS UnBundled        T-Mobile US    INROAM

但我想要这个:

PRODUCTPRICEPLANID  PRICEPLANDESCRIPTION    OPERATORNAME    SOCCODE
 95                    TMUS UnBundled        T-Mobile US     BRVC,INROAM

2 个答案:

答案 0 :(得分:0)

写为:

;WITH CTE AS 
    (SELECT  
    ppp.PRODUCTPRICEPLANID,
    ps.PRICEPLANDESCRIPTION,
    O.OPERATORNAME,
    osoc.SOCCODE
    FROM PRODUCTPRICEPLAN ppp 
    join PRICEPLANSUMMARY ps 
    on ppp.PRODUCTPRICEPLANID=ps.PRODUCTPRICEPLANID_FK 
    join PRICEPLANSOC psoc 
    on ppp.PRODUCTPRICEPLANID=psoc.ProductPricePlanID_FK
    join OPERATORSOC osoc 
    on psoc.OPERATORSOCID_FK=osoc.OPERATORSOCID
    join operators o on osoc.OPERATORID_FK=O.OPERATORID
    where ppp.PRODUCTPRICEPLANID=95)
select PRODUCTPRICEPLANID,PRICEPLANDESCRIPTION,OPERATORNAME,
    STUFF((SELECT ', ' + CAST(SOCCODE AS VARCHAR(10)) [text()]
    FROM CTE AS CTE1
    WHERE CTE1.PRODUCTPRICEPLANID = t.PRODUCTPRICEPLANID 
    AND CTE1.PRICEPLANDESCRIPTION = t.PRICEPLANDESCRIPTION
    AND CTE1.OPERATORNAME = t.OPERATORNAME
    FOR XML PATH(''), TYPE)
    .value('.','NVARCHAR(MAX)'),1,2,' ') AS SOCCODE 
from CTE t
GROUP BY  PRODUCTPRICEPLANID,PRICEPLANDESCRIPTION,OPERATORNAME

答案 1 :(得分:0)

你可以试试;

select PRODUCTPRICEPLANID,
   PRICEPLANDESCRIPTION,
   OPERATORNAME,
   LISTAGG(SOCCODE, ',') WITHIN GROUP(ORDER BY SOCCODE) text
  from (select PRODUCTPRICEPLANID,
           PRICEPLANDESCRIPTION,
           OPERATORNAME,
           SOCCODE
          from (SELECT ppp.PRODUCTPRICEPLANID,
                   ps.PRICEPLANDESCRIPTION,
                   O.OPERATORNAME,
                   osoc.SOCCODE
                 FROM PRODUCTPRICEPLAN ppp
              join PRICEPLANSUMMARY ps
                on ppp.PRODUCTPRICEPLANID = ps.PRODUCTPRICEPLANID_FK
              join PRICEPLANSOC psoc
               on ppp.PRODUCTPRICEPLANID = psoc.ProductPricePlanID_FK
              join OPERATORSOC osoc
                on psoc.OPERATORSOCID_FK = osoc.OPERATORSOCID
              join operators o
               on osoc.OPERATORID_FK = O.OPERATORID
             where ppp.PRODUCTPRICEPLANID = 95) t
     GROUP BY PRODUCTPRICEPLANID,
              PRICEPLANDESCRIPTION,
             OPERATORNAME,
              SOCCODE)
 group by PRODUCTPRICEPLANID, PRICEPLANDESCRIPTION, OPERATORNAME