如何将结果分组到一行

时间:2018-07-31 18:17:28

标签: sql sql-server

使用以下查询,我的结果是:

+----------+-------+-------+-------+-------+-------+  
| Adjuster |pending| assnd | qardy | inqa  | clsd  |  
+----------+-------+-------+-------+-------+-------+  
|   Jim    |   0   |   0   |   0   |   0   |   2   |  
+----------+-------+-------+-------+-------+-------+  
|   Jim    |   0   |   0   |   3   |   0   |   0   |      
+----------+-------+-------+-------+-------+-------+  
|   Bob    |   0   |   5   |   0   |   0   |   0   |    
+----------+-------+-------+-------+-------+-------+   
|   Bob    |   0   |   0   |   0   |   0   |   1   |    
+----------+-------+-------+-------+-------+-------+   
|   Mary   |   0   |   1   |   0   |   0   |   0   |    
+----------+-------+-------+-------+-------+-------+   
|   Mary   |   0   |   0   |   0   |   0   |   3   |    
+----------+-------+-------+-------+-------+-------+ 
|   Stacy  |   0   |   12  |   0   |   0   |   0   |    
+----------+-------+-------+-------+-------+-------+ 

我希望每个调节器仅列出一次,并将所有结果都显示在同一行中,如下例所示:

+----------+-------+-------+-------+-------+-------+  
| Adjuster |pending| assnd | qardy | inqa  | clsd  |  
+----------+-------+-------+-------+-------+-------+  
|   Jim    |   0   |   0   |   3   |   0   |   2   |  
+----------+-------+-------+-------+-------+-------+   
|   Bob    |   0   |   5   |   0   |   0   |   1   |    
+----------+-------+-------+-------+-------+-------+      
|   Mary   |   0   |   1   |   0   |   0   |   3   |    
+----------+-------+-------+-------+-------+-------+    
|   Stacy  |   0   |   12  |   0   |   0   |   0   |    
+----------+-------+-------+-------+-------+-------+ 

这是我正在使用的查询:

select  concat(anu.FirstName,' ',anu.LastName) as Adjuster
    ,case s.stat when 'Pending Assignment' then count(a.clmNo) else 0 end as pending
    ,case s.stat when 'Assigned' then count(a.clmNo) else 0 end as assnd
    ,case s.stat when 'QA Ready' then count(a.clmNo) else 0 end as qardy
    ,case s.stat when 'In QA' then count(a.clmNo) else 0 end as inqa
    ,case s.stat when 'Closed' then count(a.clmNo) else 0 end as clsd
from assnmts a 
inner join assnmtStats astats 
on a.assnmtIdPk = astats.assnmtIdFk 
inner join stats s 
on astats.aStatId = s.statIdPk 
inner join repAssnmts ra 
on a.assnmtIdPk = ra.assnmtIdFk 
inner join aspnetusers anu 
on ra.repId = anu.Id 
inner join clients c 
on a.clientIdFk = c.clientIdPk 
inner join carrs 
on a.carrierId = carrs.carrIdPk 
inner join (SELECT a2.assnmtIdPk, MAX(astats2.asCrtdDt) as MaxDate FROM assnmts a2 INNER JOIN assnmtStats astats2 on a2.assnmtIdPk =   astats2.assnmtIdFk GROUP BY a2.assnmtIdPk) mdt 
on a.assnmtIdPk = mdt.assnmtIdPk 
and astats.asCrtdDt = mdt.MaxDate 
inner join (select a3.assnmtIdPk, MAX(ra2.raCrtdDt) as MaxRepDate from assnmts a3 inner join repAssnmts ra2 on a3.assnmtIdPk = ra2.assnmtIdFk group by a3.assnmtIdPk) mrepdt 
on a.assnmtIdPk = mrepdt.assnmtIdPk 
and ra.raCrtdDt = mrepdt.MaxRepDate 
group by concat(anu.FirstName,' ',anu.LastName), s.stat
order by concat(anu.FirstName,' ',anu.LastName)

如何修改查询以获取所需格式的结果?

2 个答案:

答案 0 :(得分:3)

简便的方法是包装查询并添加您的聚合:

select 
    Adjuster
    ,sum(pending)
    ,sum(assnd)
    ,sum(qardy)
    ,sum(inqa)
    ,sum(clsd)
from(
select  concat(anu.FirstName,' ',anu.LastName) as Adjuster
    ,case s.stat when 'Pending Assignment' then count(a.clmNo) else 0 end as pending
    ,case s.stat when 'Assigned' then count(a.clmNo) else 0 end as assnd
    ,case s.stat when 'QA Ready' then count(a.clmNo) else 0 end as qardy
    ,case s.stat when 'In QA' then count(a.clmNo) else 0 end as inqa
    ,case s.stat when 'Closed' then count(a.clmNo) else 0 end as clsd
from assnmts a 
inner join assnmtStats astats 
on a.assnmtIdPk = astats.assnmtIdFk 
inner join stats s 
on astats.aStatId = s.statIdPk 
inner join repAssnmts ra 
on a.assnmtIdPk = ra.assnmtIdFk 
inner join aspnetusers anu 
on ra.repId = anu.Id 
inner join clients c 
on a.clientIdFk = c.clientIdPk 
inner join carrs 
on a.carrierId = carrs.carrIdPk 
inner join (SELECT a2.assnmtIdPk, MAX(astats2.asCrtdDt) as MaxDate FROM assnmts a2 INNER JOIN assnmtStats astats2 on a2.assnmtIdPk =   astats2.assnmtIdFk GROUP BY a2.assnmtIdPk) mdt 
on a.assnmtIdPk = mdt.assnmtIdPk 
and astats.asCrtdDt = mdt.MaxDate 
inner join (select a3.assnmtIdPk, MAX(ra2.raCrtdDt) as MaxRepDate from assnmts a3 inner join repAssnmts ra2 on a3.assnmtIdPk = ra2.assnmtIdFk group by a3.assnmtIdPk) mrepdt 
on a.assnmtIdPk = mrepdt.assnmtIdPk 
and ra.raCrtdDt = mrepdt.MaxRepDate 
group by concat(anu.FirstName,' ',anu.LastName), s.stat
) x
group by Adjuster

答案 1 :(得分:0)

您可以使用聚合:

with t as (<your query here>)
select adjuster, sum(pending) as pending, sum(assnd) as assnd, 
       sum(qardy) as qardy, sum(inqa) as inqa, sum(clsd) as clsd
from t
group by adjuster;

大概,在您的查询中,您可以使用条件聚合:

select concat(anu.FirstName, ' ', anu.LastName) as Adjuster
       sum(case s.stat when 'Pending Assignment' then a.clmNo else 0 end) as pending,
       sum(case s.stat when 'Assigned' then a.clmNo else 0 end) as assnd
       sum(case s.stat when 'QA Ready' then a.clmNo else 0 end) as qardy
       sum(case s.stat when 'In QA' then a.clmNo else 0 end) as inqa
       sum(case s.stat when 'Closed' then a.clmNo else 0 end) as clsd
from . . .
group by concat(anu.FirstName, ' ', anu.LastName);