SQL 将垂直列转换为水平列并计算值
SQL Converting Vertical Column to Horizontal and Counting Values
我真的需要你的帮助,从 SQL 的角度来看,我似乎无法像 SQL 程序员那样思考,试图获得以下结果集。
考虑下面的 table(这是我的 MS Access MDB 文件的快照:
+-----------------+-----------------+-----------------+-----------------+
| Date Received | Request Type | Branch | Division |
+-----------------+-----------------+-----------------+-----------------+
| 2016-05-10 | Status Report | Alpha | AAA |
| 2016-05-11 | Business Case | Bravo | BB |
| 2016-05-12 | Business Plan | Charlie | C |
| 2016-05-13 | Project Charter | Alpha | A |
| 2016-05-14 | Status Report | Alpha | AAA |
| 2016-05-15 | Business Plan | Charlie | CC |
| 2016-05-16 | Financial Report| Alpha | AAAA |
| 2016-05-17 | Financial Report| Alpha | AA |
| 2016-05-18 | Status Report | Bravo | BBB |
| 2016-05-19 | Financial Report| Alpha | AAA |
| 2016-05-20 | Financial Report| Bravo | B |
+-----------------+-----------------+-----------------+-----------------+
我需要你的帮助来基本捕获指标(occurrence/count 每个请求类型,然后按部门排序),从而得到如下新结果集:
+-----------------+-----------------+-----------------+-----------------+
| Division | Status Report | Business Case | Business Plan |
+-----------------+-----------------+-----------------+-----------------+
| A | 1 | 0 | 0 |
| AA | 1 | 0 | 0 |
| AAA | 1 | 0 | 0 |
| B | 0 | 1 | 0 |
| BB | 0 | 1 | 0 |
| BBB | 0 | 1 | 0 |
| C | 1 | 0 | 1 |
| CC | 1 | 0 | 1 |
| CCC | 1 | 0 | 1 |
+-----------------+-----------------+-----------------+-----------------+
编辑:计算分支的请求类型:
DECLARE @count int;
DECLARE @count0 int;
DECLARE @count1 int;
set @count = (select Count(Request Type) from TABLENAME where Request Type = 'Status Report', Division = 'AAA')
set @count0 = (select Count(Request Type) from TABLENAME where Request Type = 'Business Case', Division = 'AAA')
set @count1 = (select Count(Request Type) from TABLENAME where Request Type = 'Business Plan', Division = 'AAA')
编辑:用这个来订购
order by COLUMNXXX ASC
我真的需要你的帮助,从 SQL 的角度来看,我似乎无法像 SQL 程序员那样思考,试图获得以下结果集。
考虑下面的 table(这是我的 MS Access MDB 文件的快照:
+-----------------+-----------------+-----------------+-----------------+
| Date Received | Request Type | Branch | Division |
+-----------------+-----------------+-----------------+-----------------+
| 2016-05-10 | Status Report | Alpha | AAA |
| 2016-05-11 | Business Case | Bravo | BB |
| 2016-05-12 | Business Plan | Charlie | C |
| 2016-05-13 | Project Charter | Alpha | A |
| 2016-05-14 | Status Report | Alpha | AAA |
| 2016-05-15 | Business Plan | Charlie | CC |
| 2016-05-16 | Financial Report| Alpha | AAAA |
| 2016-05-17 | Financial Report| Alpha | AA |
| 2016-05-18 | Status Report | Bravo | BBB |
| 2016-05-19 | Financial Report| Alpha | AAA |
| 2016-05-20 | Financial Report| Bravo | B |
+-----------------+-----------------+-----------------+-----------------+
我需要你的帮助来基本捕获指标(occurrence/count 每个请求类型,然后按部门排序),从而得到如下新结果集:
+-----------------+-----------------+-----------------+-----------------+
| Division | Status Report | Business Case | Business Plan |
+-----------------+-----------------+-----------------+-----------------+
| A | 1 | 0 | 0 |
| AA | 1 | 0 | 0 |
| AAA | 1 | 0 | 0 |
| B | 0 | 1 | 0 |
| BB | 0 | 1 | 0 |
| BBB | 0 | 1 | 0 |
| C | 1 | 0 | 1 |
| CC | 1 | 0 | 1 |
| CCC | 1 | 0 | 1 |
+-----------------+-----------------+-----------------+-----------------+
编辑:计算分支的请求类型:
DECLARE @count int;
DECLARE @count0 int;
DECLARE @count1 int;
set @count = (select Count(Request Type) from TABLENAME where Request Type = 'Status Report', Division = 'AAA')
set @count0 = (select Count(Request Type) from TABLENAME where Request Type = 'Business Case', Division = 'AAA')
set @count1 = (select Count(Request Type) from TABLENAME where Request Type = 'Business Plan', Division = 'AAA')
编辑:用这个来订购
order by COLUMNXXX ASC