繁体   English   中英

SQL将垂直列转换为水平和计数值

[英]SQL Converting Vertical Column to Horizontal and Counting Values

我确实需要您的帮助,并且从SQL的角度来看,我似乎不太想像SQL程序员那样尝试获取以下结果集。

请考虑下表(这是我的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         |
+-----------------+-----------------+-----------------+-----------------+

我需要您的帮助才能基本捕获指标(每种请求类型的出现次数/计数,然后按部门进行排序),从而得到以下新结果集:

+-----------------+-----------------+-----------------+-----------------+
|    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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM