2012-11-01 293 views
1

嗨我有下面的sql輸出。在同一列中總結兩種不同的類型

TYPE SUM(UPDATE_COUNT) 
Legis 93 
Acds 43 
Updates 41 
Multibases 345 

但我希望更新顯示爲更新和多基的總和,這裏多基和更新屬於同一列。它應該像下面那樣。

Updates 386 

我用的第一輸出的查詢是

SELECT type, SUM(Update_Count) 
FROM Scope1 
where type in ('Updates','Multibases','DAIS','Acds','Legis','LegAll') and 
     (RECVD_DATE >='04/02/2012' and RECVD_DATE <='11/30/2012') 
group by type 

請幫助我如何獲得第二輸出。

回答

3
SELECT 
    CASE WHEN type IN 'Updates','Multibases' THEN 'Updates' ELSE type END as TYPE, 
    SUM(Update_Count) 
FROM 
    Scope1 
WHERE 
     type in ('Updates','Multibases','DAIS','Acds','Legis','LegAll') 
    and (RECVD_DATE >='04/02/2012' and RECVD_DATE <='11/30/2012') 
GROUP BY 
    CASE WHEN type IN 'Updates','Multibases' THEN 'Updates' ELSE type END 

更具擴展性的解決方案有另一張地圖表s typesuper_type

SELECT 
    map.super_type, 
    SUM(Scope1.update_count) 
FROM 
    Scope1 
INNER JOIN 
    map 
    ON map.type = Scope1.type 
WHERE 
     Scope1.type in ('Updates','Multibases','DAIS','Acds','Legis','LegAll') 
    and Scope1.RECVD_DATE >='04/02/2012' 
    and Scope1.RECVD_DATE <='11/30/2012' 
GROUP BY 
    map.super_type 

然後你就可以把條目,如在地圖下面...

type  | super_type 
------------+------------ 
Legis  | Legis 
Acds  | Acds 
Updates | Updates 
Multibases | Updates 

等,等

+0

感謝您的回答好友 – Rakesh

1
SELECT type, SUM(Update_Count) 
FROM Scope1 
where type in ('DAIS','Acds','Legis','LegAll') and 
     (RECVD_DATE >='04/02/2012' and RECVD_DATE <='11/30/2012') 
group by type 
UNION ALL 
SELECT 'Updates' AS type, SUM(Update_Count) 
FROM Scope1 
where type in ('Updates','Multibases') and 
     (RECVD_DATE >='04/02/2012' and RECVD_DATE <='11/30/2012') 

將產生如下:

 
TYPE SUM(UPDATE_COUNT) 
Legis 93 
Acds 43 
Updates 386 

或者你也可以使用一個CASE產生相同的輸出:

SELECT CASE WHEN type IN ('Updates','Multibases') THEN 'Updates' ELSE type END AS type, SUM(Update_Count) 
FROM Scope1 
where type in ('Updates','Multibases','DAIS','Acds','Legis','LegAll') and 
     (RECVD_DATE >='04/02/2012' and RECVD_DATE <='11/30/2012') 
group by CASE WHEN type IN ('Updates','Multibases') THEN 'Updates' ELSE type END 
+0

感謝隊友的答案 – Rakesh

2
SELECT type, SUM(case when type in ('Updates','Multibases') then Update_Count else o end) as Update_Count 
FROM Scope1 
where type in ('Updates','Multibases','DAIS','Acds','Legis','LegAll') and 
     (RECVD_DATE >='04/02/2012' and RECVD_DATE <='11/30/2012') 
group by type 
+0

這仍然會爲每種類型一行;它不會將'Updates'和'Multibases'合併成一行,這就是OP想要的。而且,在SUM()中的'CASE'語句將強制'Update_Count'結果爲'0',因爲每個不屬於'in'('Updates','Multibases')的'類型'。 * [如果你有'ELSE 0'而不是'ELSE o',至少會這樣。] * – MatBailie

2

如果這是一次性的,然後使用的一個其他方案。這裏有一個數據驅動的主意,如果你可能會改變你的分組未來:

Create Table TypeBucket (
    type varchar(50) not null primary key, 
    bucket varchar(50) not null 
); 

Insert Into TypeBucket (type, bucket) values 
    ('Legis', 'Legis'), 
    ('Acds', 'Acds'), 
    ('Updates', 'Updates'), 
    ('Multibases', 'Updates'), 
    ('LegAll', 'LegAll'), 
    ('DAIS', 'DAIS'); 

Select 
    b.bucket, 
    Sum(s.update_count) 
From 
    Scope1 s 
    Inner Join 
    TypeBucket b 
    On s.type = b.Type 
Where 
    s.type in ('Updates', 'Multibases', 'DAIS', 'Acds', 'Legis', 'LegAll') And 
    s.recvd_date >= '04/02/2012' And 
    s.recvd_date < '12/1/2012' 
Group By 
    b.bucket 
相關問題