2017-01-05 13 views
0

以下の2つのテーブルがあります。最初のテーブルのカシッドは、事故とともに2番目のテーブルで参照されます。私は、事件の種類ごとに違った事故を起こそうとしています。 2つのテーブルの下で、私はサンプルデータと期待される結果を記録しました。対応する行の総計のSQLクエリの合計

場合

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

何か試してみましたか? –

答えて

2

評価の合計を得るには、SUM(CASE WHEN)節を使用して、評価に一致するすべてのレコードを1つ追加します。

あなたの質問には、すべてCaseTypeという別個のものを見たいと思っています。正しいジョインを使って取得できます。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

+0

Upvoted 'Right Join';) –

0

あなたが前にselect句でケースを使用することがありますか?

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

はこれが役立つことを願っています!

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 
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 
関連する問題