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

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

4

4 回答 4

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

一个更具可扩展性的解决方案是让另一个表映射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

等等等等

于 2012-11-01T11:44:27.790 回答
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
于 2012-11-01T11:44:50.650 回答
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
于 2012-11-01T11:51:18.243 回答
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') 

将产生以下内容:

类型总和(UPDATE_COUNT)
立法 93
使徒行传 43
更新 386

或者您也可以使用 aCASE来产生相同的输出:

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
于 2012-11-01T11:44:17.463 回答