Как объединить результирующий набор в одну строку после использования group by - PullRequest
0 голосов
/ 23 октября 2018

Сценарий 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 голосов
/ 23 октября 2018

Вы можете попытаться использовать 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

0 голосов
/ 23 октября 2018

обслуживающий персонал

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
...