【发布时间】:2018-10-19 22:44:37
【问题描述】:
我有一个名为行业的表。有6个字段。架构如下。
在这种情况下,我需要执行自定义聚合。数据库中有 22 个区域。需要进行两个自定义聚合:
- 需要将区域 1-17 合并为一个值为 00 的新区域。
- 区域 20 和 21 需要改成另一个区域,代码值为 99。
接下来是我对此总体框架的尝试。我假设创建一个新表是完成此操作的最简单方法。底部是预期结果的一个非常简短的示例。
create table industry2
(
year char(4),
qtr char(2),
area char(6),
industry char(3),
ownership char(2),
employment numeric(8,0)
);
INSERT INTO Industry2
(year, qtr, area, industry, ownership, employment)
SELECT year, qtr, area, (select sum (employment) from dbo.industry where area
= '01' or area = '02' and so on):
2017 01 01 123000 1 456
2017 01 02 123000 1 101
2017 01 03 123000 1 103
2017 01 01 134000 1 6
2017 01 02 134000 1 7
2017 01 03 134000 1 12
2017 01 09 134000 1 1
2017 01 01 144000 1 14
2017 01 20 134000 1 7
2017 01 21 134000 1 8
预期结果
2017 01 00 123000 1 660
2017 01 00 134000 1 26
2017 01 00 144000 1 14
2017 01 99 134000 1 15
【问题讨论】:
标签: sql-server tsql ssms aggregate-functions