Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 使用group by后如何将结果集合并为一行_Sql_Sql Server_Group By_Sql Server 2014 - Fatal编程技术网

Sql 使用group by后如何将结果集合并为一行

Sql 使用group by后如何将结果集合并为一行,sql,sql-server,group-by,sql-server-2014,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 cons

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
如何实现上述目标?

使用员工

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

您可以尝试将
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

这是一个很好的健壮解决方案,具有很大的灵活性,是SQLFIDLE的一个很好的演示approach@D-施。这不使用
stuff()
进行字符串连接。查看
stuff()
正在做什么。这是使用
作为xml路径
进行字符串连接,并使用
stuff()
进行细微的美学调整。