Oracle SQL计数并按多个字段分组

时间:2018-08-17 04:37:23

标签: sql oracle count grouping

我能够合并两个表的数据来获得下表。

+------------+------+--------+--------+------------+------------+
| Group Name | Type | Manger | Status | ControlOne | ControlTwo |
+------------+------+--------+--------+------------+------------+
| Group A    |    1 |      1 | finish |          2 |          2 |
| Group A    |    2 |      1 | open   |          0 |          2 |
| Group A    |    1 |      1 | finish |          0 |          0 |
| Group A    |    1 |      2 | finish |          2 |          0 |
| Group B    |    1 |      1 | open   |          2 |          0 |
| Group B    |    1 |      2 | open   |          2 |          2 |
| Group B    |    2 |      2 | open   |          0 |          2 |
| Group B    |    2 |      1 | finish |          0 |          0 |
| Group B    |    1 |      1 | open   |          2 |          0 |
+------------+------+--------+--------+------------+------------+

现在,我需要基于GroupName / Type和Manager获取总数,以使每个组的输出采用以下格式:

+------------+------+-------------------------------------------------+--------------------------------------------+------------------------------+----------------------------+
| Group Name | Type |                 Manager1Finish                  |                Manager1Open                |        Manager2Finish        |        Manager2Open        |
+------------+------+-------------------------------------------------+--------------------------------------------+------------------------------+----------------------------+
| Group A    |    1 | 2(count of finish by Group A, manager1, type 1) | 0(count of open Manager1, Type 1, Group A) | 1(count of finish Manager 2) | 0(count of open manager 2) |
| Group A    |    2 | 0                                               | 1                                          | 0                            | 0                          |
+------------+------+-------------------------------------------------+--------------------------------------------+------------------------------+----------------------------+

请帮助您实现这一目标吗?

2 个答案:

答案 0 :(得分:3)

请尝试以下情况:

  SELECT GroupName,
         TYPE,
         COUNT (CASE
                   WHEN     Manager = 1
                        AND status = 'Finish'
                   THEN
                      1
                END)
            AS Manager1Finish,
         COUNT (CASE
                   WHEN     Manager = 1
                        AND status = 'Open'
                   THEN
                      1
                END)
            AS Manager1Open,
         COUNT (CASE
                   WHEN     Manager = 2
                        AND status = 'Finish'
                   THEN
                      1
                END)
            AS Manager2Finish,
         COUNT (CASE
                   WHEN     Manager = 2
                        AND status = 'Open'
                   THEN
                      2
                END)
            AS Manager2Open
    FROM tablename
GROUP BY GroupName, TYPE

答案 1 :(得分:2)

select [group], [type],
sum(case when manager=1 and status='finish' then 1 else 0 end) as m1finish,
sum(case when manager=1 and status='open' then 1 else 0 end) as m1open,
sum(...etc...)
from mytable
group by [group],[type]