2012-07-18 51 views
0

我試圖獲得傳遞和失敗設備的計數我擁有所有我想要的數字,但它們並不完全正確。按條件計算

例如:

select 
    t3.displayname as [PSI], 
    case when t0.compliant = 0 then count(displayname) end as [failures], 
    case when t0.compliant = 1 then count(displayname) end as [success] 
from lineitemsmap as t0 
    inner join art_blob as t1 on t1.art_blob_id = t0.blobid 
    inner join art_asset as t2 on t2.art_asset_id = t1.art_asset_id 
    inner join net_ou as t3 on t3.net_ouid = t2.net_ouid 
group by t3.displayname, t0.compliant 

生成:

------------------------------ 
| PSI | failures | success | 
------------------------------ 
| 1 | 3  | NULL  | 
------------------------------ 
| 2 | 4  | NULL  | 
------------------------------ 
| 3 | 5  | NULL  | 
------------------------------ 
| 1 | NULL  | 6   | 
------------------------------ 
| 2 | NULL  | 7   | 
------------------------------ 
| 3 | NULL  | 8   | 
------------------------------ 

我試圖讓所有的結果,在3行返回第6節

回答

3
select 
    t3.displayname as [PSI], 
    SUM(case when t0.compliant = 0 then 1 else 0 end) as [failures], 
    SUM(case when t0.compliant = 1 then 1 else 0 end) as [success] 
from lineitemsmap as t0 
    inner join art_blob as t1 on t1.art_blob_id = t0.blobid 
    inner join art_asset as t2 on t2.art_asset_id = t1.art_asset_id 
    inner join net_ou as t3 on t3.net_ouid = t2.net_ouid 
group by t3.displayname;