SQL查询总对应行的总和

时间:2017-01-05 01:51:11

标签: sql sql-server tsql

我有两张桌子如下。来自第一张表的Caseid在第二张表中被引用以及事故。我试图获得一个案例类型的不同事故。在下面的两个表中,我记录了样本数据和预期结果。

案例

caseId     CaseType
 1            AB
 2            AB
 3            AB
 4            CD
 5            CD
 6            DE

CaseAccidents

AccidentId   caseID    AccidentRating
1         1       High
2         1       High
3         1       Medium    
4         1       LOW
5         2       High
6         2       Medium    
7         2       LOW
8         5       High
9         5       High
10        5       Medium    
11        5       LOW

结果应如下所示:

CaseType TotalHIghrating    TotalMediumRating   TotalLOWRating
AB           3          2           2
CD           2          1           1
DE           0          0           0

5 个答案:

答案 0 :(得分:2)

要获得每个评级的总和,您可以使用SUM(CASE WHEN)子句,每个匹配评级的记录加1。

在您的问题中,您已经指出要查看所有不同的CaseType,您可以使用RIGHT JOIN获取它,这将包括case表的所有记录。

select case.CaseType,
       sum(case when caseAccidents.AccidentRating = 'High' then 1 else 0 end) as TotalHighRating,
       sum(case when caseAccidents.AccidentRating = 'Medium' then 1 else 0 end) as TotalMediumRating,
       sum(case when caseAccidents.AccidentRating = 'LOW' then 1 else 0 end) as TotalLowRating
from caseAccidents
     right join case on case.caseId = caseAccidents.caseID
group by case.CaseType;

+----------+-----------------+-------------------+----------------+
| CaseType | TotalHighRating | TotalMediumRating | TotalLowRating |
+----------+-----------------+-------------------+----------------+
|    AB    |        3        |         2         |        2       |
+----------+-----------------+-------------------+----------------+
|    CD    |        2        |         1         |        1       |
+----------+-----------------+-------------------+----------------+
|    DE    |        0        |         0         |        0       |
+----------+-----------------+-------------------+----------------+

检查:http://rextester.com/MCGJA9193

答案 1 :(得分:0)

您之前是否在select子句中使用了case?

select C.CaseType, 
sum(case when CA.AccidentRating = 'High' then 1 else 0 end)
from Case C join CaseAccidents CA on C.CaseId = CA.CaseId
group by C.CaseType

答案 2 :(得分:0)

请看这个。表的示例查询以及结果

create table #case(caseid int,casetype varchar(5))
  insert into #case (caseid,casetype)
   select 1,'AB' union all
   select 2,'AB' union all
   select 3,'AB' union all
   select 4,'CD' union all
   select 5,'CD' union all
   select 6,'DE' 

create table #CaseAccidents(AccidentId int, CaseId int,AccidentRating varchar(10))
insert into #CaseAccidents(AccidentId, CaseId, AccidentRating)
 select 1,1,'High' union all
 select 2,1,'High' union all
 select 3,1,'Medium' union all
 select 4,1,'Low' union all
 select 5,2,'High' union all
 select 6,2,'Medium' union all
 select 7,2,'Low' union all
 select 8,5,'High' union all
 select 9,5,'High' union all
 select 10,5,'Medium' union all
select 11,5,'Low'

我的剧本

select c.casetype,
   sum(case when ca.AccidentRating='High' then 1 else 0 end) as TotalHighRating,
   sum(case when ca.AccidentRating='Medium' then 1 else 0 end) as TotalMediumRating,
   sum(case when ca.AccidentRating='Low' then 1 else 0 end) as TotalLowRating   
    from #case c
    Left join #CaseAccidents ca
    on c.Caseid=ca.Caseid
    group by c.casetype

希望这可能有所帮助!

答案 3 :(得分:0)

使用Pivot运算符

的另一种方法
SELECT casetype,
       [High],
       [Medium],
       [Low]
FROM   (SELECT c.casetype,
               AccidentRating
        FROM   case c
               LEFT JOIN CaseAccidents ca
                      ON ca.CaseId = c.caseid)a
       PIVOT (Count(AccidentRating)
             FOR AccidentRating IN ([High],
                                    [Medium],
                                    [Low]) ) p 

答案 4 :(得分:0)

尝试此代码一次。

select casetype,
sum(case when ca.AccidentRating='High' then 1 else 0 end ) as TotalHIghrating,
sum(case when ca.AccidentRating='Medium' then 1 else 0 end ) as TotalMediumRating   ,
sum(case when ca.AccidentRating='Low' then 1 else 0 end ) as TotalLOWRating
from #case c
left join #CaseAccidents ca on c.caseid=ca.CaseId
group by casetype