简体   繁体   中英

SQL query sum of total corresponding rows

I have two tables as below. Caseid from first table is referenced in second table along with accidents. What I am trying to get total different accidents for a case type. Below two tables I documented sample data and expected result.

Table case :

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

Table 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

Result should look like:

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

To get the sum of every rating, you can Use a SUM(CASE WHEN) clause, adding 1 by every record that match the rating.

In your question, you have pointed out that you want to see all distinct CaseType , you can get it by using a RIGHT JOIN, this will include all records of case table.

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

Check it: http://rextester.com/MCGJA9193

Have you use case in a select clause before?

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

Please see this. Sample query of the table and also that result

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'

My script

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

Hope This could help!

Another approach using Pivot operator

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 

Try This code once.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM