简体   繁体   English

使用 GROUP BY ROLLUP 组合字段

[英]Combining fields with GROUP BY ROLLUP

I want to have a query that shows every contract made in my company and have it grouped by function.我想要一个查询,显示我公司签订的每份合同,并将其按 function 分组。

My problem is that I have a lot of functions, for instance:我的问题是我有很多功能,例如:

  • Partner伙伴
  • Seller卖方
  • Manager经理
  • Captain队长
  • Etc.等等。

I want to show 3 rows, one with all contracts from "Partners", other by all the "Interns" (everyone that isn't a partner), and last one with a Total of the two, achieved with the ROLLUP .我想显示 3 行,一行包含来自“合作伙伴”的所有合同,另一行包含所有“实习生”(不是合作伙伴的每个人)的合同,最后一行包含两者的总和,通过ROLLUP实现。 But I can't seem to only show this 3 rows, my query turns everything that is not partner to be name "intern", but doesn't combine this cells, so I get this result:但我似乎不能只显示这 3 行,我的查询将所有不是合作伙伴的东西都命名为“实习生”,但不合并这些单元格,所以我得到了这个结果:

+--------------+-------------+--------------+---------+
| Seller Type  |  Installed  |   Scheduled  |  Total  |
+--------------+-------------+--------------+---------+
| Intern       |      1      |      0       |    1    |
+--------------+-------------+--------------+---------+
| Intern       |      3      |      0       |    3    |
+--------------+-------------+--------------+---------+
| Partner      |     10      |      5       |    15   |
+--------------+-------------+--------------+---------+
| Intern       |     19      |      10      |    29   |
+--------------+-------------+--------------+---------+
| Total        |     33      |      15      |    48   |
+--------------+-------------+--------------+---------+

And my goal was to Group everyone that isn't a "partner" into the same row, and show it like this:我的目标是将所有不是“合作伙伴”的人分组到同一行,并像这样显示:

+--------------+-------------+--------------+---------+
| Seller Type  |  Installed  |   Scheduled  |  Total  |
+--------------+-------------+--------------+---------+
| Partner      |     10      |      5       |    15   |
+--------------+-------------+--------------+---------+
| Intern       |     23      |      10      |    33   |
+--------------+-------------+--------------+---------+
| Total        |     33      |      15      |    48   |
+--------------+-------------+--------------+---------+

My data for table CM3我的表CM3的数据

+--------------+-------------+--------------+
| u_func       |     cm      |     name     |
+--------------+-------------+--------------+
| Captain      |      1      |     Chris    |
+--------------+-------------+--------------+
| Manager      |      2      |     Mary     |
+--------------+-------------+--------------+
| Partner      |      3      |    Anthony   |
+--------------+-------------+--------------+
| Seller       |      4      |    Hannah    |
+--------------+-------------+--------------+

My data for table BO我的表BO的数据

+--------------+-------------+--------------+
| stamp        |    seller   |      sta     |
+--------------+-------------+--------------+
| PO109832910  |      1      |   Installed  |
+--------------+-------------+--------------+
| PO389213201  |      2      |   Installed  |
+--------------+-------------+--------------+
| PO930821639  |      3      |   Scheduled  |
+--------------+-------------+--------------+
| PO987583213  |      4      |   Scheduled  |
+--------------+-------------+--------------+

My query is我的查询是

SELECT CASE 
          WHEN GROUPING(CM3.u_func)=1 THEN 'TOTAL'
          WHEN CM3.u_func <> 'Partner' THEN 'Intern'
       END "Seller Type"
, COUNT(CASE 
          WHEN BO.stat = 'Installed' THEN 1 
       END) "Installed"
, COUNT(CASE
          WHEN BO.stat = 'Scheduled' THEN 1
       END) "Scheduled"
, COUNT(CASE
          WHEN BO.stat IN ('Installed','Scheduled') THEN 1
       END) "Total"
FROM BO
JOIN CM3 ON cm3.cm = BO.seller
GROUP BY ROLLUP(CM3.u_func)

You need your GROUP BY to mirror the logic in your SELECT statement (which also needs some adjustment):您需要GROUP BY来反映SELECT语句中的逻辑(也需要进行一些调整):

SELECT CASE 
      WHEN GROUPING
      (CASE 
      WHEN CM3.u_func = 'Partner' THEN CM3.u_func 
      ELSE 'Intern'
   END) = 1 THEN 'TOTAL'
   ELSE CASE 
      WHEN CM3.u_func = 'Partner' THEN CM3.u_func 
      ELSE 'Intern'
   END 
   END "Seller Type"
, COUNT(CASE 
      WHEN BO.stat = 'Installed' THEN 1 
   END) "Installed"
, COUNT(CASE
      WHEN BO.stat = 'Scheduled' THEN 1
   END) "Scheduled"
, COUNT(CASE
      WHEN BO.stat IN ('Installed','Scheduled') THEN 1
   END) "Total"
FROM BO
JOIN CM3 ON cm3.cm = BO.seller
GROUP BY CASE 
      WHEN CM3.u_func = 'Partner' THEN CM3.u_func ELSE 'Intern'
   END WITH ROLLUP

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

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