在同一列中汇总两种不同的类型

时间:2012-11-01 11:41:23

标签: sql jdbc oracle10g

嗨我有以下作为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

请帮助我如何获得第二个输出。

4 个答案:

答案 0 :(得分: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

更具伸缩性的解决方案是使用另一个表格将type映射到super_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

等等

答案 1 :(得分: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

答案 2 :(得分: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

答案 3 :(得分: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
相关问题