1

3 つのテーブルを結合するクエリを作成する必要があります。私は次の表を持っています。

    Table Case
    CaseID  CaseType   CaseStatus
    001       1         2        
    002       2         1        
    003       3         4        

    Table Case Type
    TypeID    Name   
    1       Fraud            
    2       Duplicate    
    3       Other

最後にケースのステータス

    StatusID      Name   
    1             Resolved             
    2             Pending     
    3             Waiting   

私はこのような結果が必要です

    Type          NumberofCase   Resolved    Pending   Waiting
    Fraud         1              1            0         0
    Duplicate     2              0            1         1
    Other         4              2            1         1

一時テーブルを使用したくない場合はどうすればよいですか。Col-case またはその他の方法でこれを実現する方法。

4

2 に答える 2

2

join条件付き集計を使用してこれを行うことができます。

select ct.Name as "Type", count(*) as NumberOfCase,
       sum(case when cs.Name = 'Resolved' then 1 else 0 end) as Resolved,
       sum(case when cs.Name = 'Pending' then 1 else 0 end) as Pending,
       sum(case when cs.Name = 'Waiting' then 1 else 0 end) as Waiting
from "case" c join
     CaseType ct
     on c.CaseType = ct.TypeId join
     CaseStatus cs
     on c.CaseStatus = cs.StatusId
group by ct.Name;
于 2013-09-17T11:14:42.747 に答える