使用分组方式后如何将结果集合并为一行

时间:2018-10-23 10:00:05

标签: sql sql-server group-by sql-server-2014

SQL脚本:

select can.Reference, can.CandidateID, can.firstname + ' ' + can.surname AS 'Candidate',
con.firstname + ' ' + con.lastname as 'Consultant', sector.unitname from candidate can
inner join address ad on can.address = ad.addressid
inner join consultants con on con.consultantid = can.owningconsultant
inner join client cl on cl.ownedby = con.consultantid
inner join clientdata cd on cd.clientid = cl.clientid
inner join businessunits sector on sector.unitid = cd.ClientSectorID
where can.division = 1
and
can.OwningConsultant = 385
and 
can.status in ('56','179') 
group by can.Reference, can.CandidateID, can.FirstName, can.Surname, con.FirstName, con.LastName, can.Created, sector.unitname
order by can.created desc

结果集:

Ref     CanID   CanName     ConName             Sector
Bob1    188435  Eve Evil    Charlie Chaplin     Nursery
Bob1    188435  Eve Evil    Charlie Chaplin     Private Schools
Bob1    188435  Eve Evil    Charlie Chaplin     Secondary
Bob1    188435  Eve Evil    Charlie Chaplin     SEN

在上面,您可以看到返回了4个结果,如果有多个要说的话,我希望返回该结果:

Ref     CanID   CanName     ConName             Sector
Bob1    188435  Eve Evil    Charlie Chaplin     Nursery, Private Schools, Secondary, SEN

如何实现以上目标?

2 个答案:

答案 0 :(得分:1)

使用人员

with  YourTable as
(
select can.Reference, can.CandidateID, can.firstname + ' ' + can.surname AS 'Candidate',
con.firstname + ' ' + con.lastname as 'Consultant', sector.unitname from candidate can
inner join address ad on can.address = ad.addressid
inner join consultants con on con.consultantid = can.owningconsultant
inner join client cl on cl.ownedby = con.consultantid
inner join clientdata cd on cd.clientid = cl.clientid
inner join businessunits sector on sector.unitid = cd.ClientSectorID
where can.division = 1
and
can.OwningConsultant = 385
and 
can.status in ('56','179') 
group by can.Reference, can.CandidateID, can.FirstName, can.Surname, con.FirstName, con.LastName, can.Created, sector.unitname
)

SELECT 
  Ref,CanID,CanName,ConName,
  STUFF((
    SELECT ', ' + [Sector] + ',' + CAST([Value] AS VARCHAR(MAX)) 
    FROM YourTable 
    WHERE (CanID = Results.CanID and Ref=Results.Ref and CanName=Results.CabName and ConName=Results.ConName) 
    FOR XML PATH(''),TYPE).value('(./text())[1]','VARCHAR(MAX)')
  ,1,2,'') AS SectorValues
FROM YourTable Results
GROUP BY Ref,CanID,CanName,ConName

答案 1 :(得分:1)

您可以尝试将STUFF与cte结合使用。

;with cte as(
    select 
        can.Reference, 
        can.CandidateID,
        can.firstname + ' ' + can.surname AS 'Candidate',
        con.firstname + ' ' + con.lastname as 'Consultant',
        sector.unitname  'Sector' 
    from candidate can
        inner join address ad on can.address = ad.addressid
        inner join consultants con on con.consultantid = can.owningconsultant
        inner join client cl on cl.ownedby = con.consultantid
        inner join clientdata cd on cd.clientid = cl.clientid
        inner join businessunits sector on sector.unitid = cd.ClientSectorID
    where can.division = 1
    and
    can.OwningConsultant = 385
    and 
    can.status in ('56','179') 
    group by can.Reference, can.CandidateID, can.FirstName, can.Surname, con.FirstName, con.LastName, can.Created, sector.unitname
)
SELECT distinct
    Reference,
    CandidateID,
    Candidate,
    Consultant,
    STUFF((
    SELECT  ','+ Sector
    FROM cte tt
    FOR XML PATH(''),TYPE ).value('.','VARCHAR(MAX)'),1,1,'') 
FROM cte t1

sqlfiddle