2012-11-01 111 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 
相关问题