目录
一、问题引入
二、with rollup查询
三、去掉多余数据
四、拓展
一、问题引入
查询SQL后结果如下,字段分别是用户、项目、批次、工作时间:
SELECT UserID,ProjectID,ProBatchesID,WorkHour
FROM MAINTABLE
GROUP BY
HourFiller
,ProjectID
,ProBatchesID
结果:
UserID | ProjectID | ProBatchesID | WorkHour |
0400bb0e-a957-479e-98e0-60c4542eefa6 | E963F8E2-4713-4B7C-A154-CC4ED75A0CAA | 0C00F223-F9B7-4C23-A762-CC8A23D25F2C | 72 |
0400bb0e-a957-479e-98e0-60c4542eefa6 | E963F8E2-4713-4B7C-A154-CC4ED75A0CAA | 596BDF31-0038-4C36-AA95-B03F3D9C7B06 | 224 |
052a3c6e-3a0e-44d2-8d5c-177760d03768 | E963F8E2-4713-4B7C-A154-CC4ED75A0CAA | 0C00F223-F9B7-4C23-A762-CC8A23D25F2C | 72 |
052a3c6e-3a0e-44d2-8d5c-177760d03768 | E963F8E2-4713-4B7C-A154-CC4ED75A0CAA | 596BDF31-0038-4C36-AA95-B03F3D9C7B06 | 224 |
056f117a-163e-4cd9-acf1-362837c9904a | 53aa4cf1-99bf-46a0-acd6-11fc5b28e076 | 1accce0a-1d4e-4e73-aa6f-9e2da0337f60 | 8 |
056f117a-163e-4cd9-acf1-362837c9904a | 53aa4cf1-99bf-46a0-acd6-11fc5b28e076 | f3f3e46b-8ca8-44cb-a4b5-5a8970f905ba | 2 |
056f117a-163e-4cd9-acf1-362837c9904a | 53aa4cf1-99bf-46a0-acd6-11fc5b28e076 | f719e55b-8142-43a7-a865-29e053d05660 | 24 |
071632d3-65ae-4f13-b45d-e158e46bbf24 | E963F8E2-4713-4B7C-A154-CC4ED75A0CAA | 0C00F223-F9B7-4C23-A762-CC8A23D25F2C | 16 |
071632d3-65ae-4f13-b45d-e158e46bbf24 | E963F8E2-4713-4B7C-A154-CC4ED75A0CAA | 596BDF31-0038-4C36-AA95-B03F3D9C7B06 | 48 |
我要的需求是:对每个人每个项目做一个WorkHour的合计,如何运用with rollup实现
二、with rollup查询
WITH TEMP AS(SELECT UserID,ProjectID,ProBatchesID,WorkHour FROM MAINTABLE GROUP BYHourFiller,ProjectID ,ProBatchesID WITH ROLLUP
)
SELECT * FROM TEMP
结果:
UserID | ProjectID | ProBatchesID | WorkHour |
0400bb0e-a957-479e-98e0-60c4542eefa6 | E963F8E2-4713-4B7C-A154-CC4ED75A0CAA | 0C00F223-F9B7-4C23-A762-CC8A23D25F2C | 72 |
0400bb0e-a957-479e-98e0-60c4542eefa6 | E963F8E2-4713-4B7C-A154-CC4ED75A0CAA | 596BDF31-0038-4C36-AA95-B03F3D9C7B06 | 224 |
0400bb0e-a957-479e-98e0-60c4542eefa6 | E963F8E2-4713-4B7C-A154-CC4ED75A0CAA | NULL | 296 |
0400bb0e-a957-479e-98e0-60c4542eefa6 | NULL | NULL | 296 |
052a3c6e-3a0e-44d2-8d5c-177760d03768 | E963F8E2-4713-4B7C-A154-CC4ED75A0CAA | 0C00F223-F9B7-4C23-A762-CC8A23D25F2C | 72 |
052a3c6e-3a0e-44d2-8d5c-177760d03768 | E963F8E2-4713-4B7C-A154-CC4ED75A0CAA | 596BDF31-0038-4C36-AA95-B03F3D9C7B06 | 224 |
052a3c6e-3a0e-44d2-8d5c-177760d03768 | E963F8E2-4713-4B7C-A154-CC4ED75A0CAA | NULL | 296 |
052a3c6e-3a0e-44d2-8d5c-177760d03768 | NULL | NULL | 296 |
056f117a-163e-4cd9-acf1-362837c9904a | 53aa4cf1-99bf-46a0-acd6-11fc5b28e076 | 1accce0a-1d4e-4e73-aa6f-9e2da0337f60 | 8 |
056f117a-163e-4cd9-acf1-362837c9904a | 53aa4cf1-99bf-46a0-acd6-11fc5b28e076 | f3f3e46b-8ca8-44cb-a4b5-5a8970f905ba | 2 |
056f117a-163e-4cd9-acf1-362837c9904a | 53aa4cf1-99bf-46a0-acd6-11fc5b28e076 | f719e55b-8142-43a7-a865-29e053d05660 | 24 |
056f117a-163e-4cd9-acf1-362837c9904a | 53aa4cf1-99bf-46a0-acd6-11fc5b28e076 | NULL | 34 |
056f117a-163e-4cd9-acf1-362837c9904a | NULL | NULL | 34 |
071632d3-65ae-4f13-b45d-e158e46bbf24 | E963F8E2-4713-4B7C-A154-CC4ED75A0CAA | 0C00F223-F9B7-4C23-A762-CC8A23D25F2C | 16 |
071632d3-65ae-4f13-b45d-e158e46bbf24 | E963F8E2-4713-4B7C-A154-CC4ED75A0CAA | 596BDF31-0038-4C36-AA95-B03F3D9C7B06 | 48 |
071632d3-65ae-4f13-b45d-e158e46bbf24 | E963F8E2-4713-4B7C-A154-CC4ED75A0CAA | NULL | 64 |
071632d3-65ae-4f13-b45d-e158e46bbf24 | NULL | NULL | 64 |
发现结果中不同人不同项目会自动分组合计,因此我们去掉多余的合计数据即可。
三、去掉多余数据
WITH TEMP AS(SELECT UserID,ProjectID,ProBatchesID,WorkHour FROM MAINTABLE GROUP BYHourFiller,ProjectID ,ProBatchesID WITH ROLLUP
)
SELECT
CASE WHEN ProBatchesID IS NULL THEN 'Total' ELSE UserID END AS UserID /*列出子数据和总合计*/
,CASE WHEN ProBatchesID IS NULL THEN 'Total' ELSE ProjectID END AS ProjectID
,CASE WHEN ProBatchesID IS NULL THEN 'Total' ELSE ProBatchesID END AS ProBatchesID
,WorkHour
FROM TEMP
WHERE ProjectID IS NOT NULL /*去掉多余的合计数据*/
结果:
UserID | ProjectID | ProBatchesID | WorkHour |
0400bb0e-a957-479e-98e0-60c4542eefa6 | E963F8E2-4713-4B7C-A154-CC4ED75A0CAA | 0C00F223-F9B7-4C23-A762-CC8A23D25F2C | 72 |
0400bb0e-a957-479e-98e0-60c4542eefa6 | E963F8E2-4713-4B7C-A154-CC4ED75A0CAA | 596BDF31-0038-4C36-AA95-B03F3D9C7B06 | 224 |
Total | Total | Total | 296 |
052a3c6e-3a0e-44d2-8d5c-177760d03768 | E963F8E2-4713-4B7C-A154-CC4ED75A0CAA | 0C00F223-F9B7-4C23-A762-CC8A23D25F2C | 72 |
052a3c6e-3a0e-44d2-8d5c-177760d03768 | E963F8E2-4713-4B7C-A154-CC4ED75A0CAA | 596BDF31-0038-4C36-AA95-B03F3D9C7B06 | 224 |
Total | Total | Total | 296 |
056f117a-163e-4cd9-acf1-362837c9904a | 53aa4cf1-99bf-46a0-acd6-11fc5b28e076 | 1accce0a-1d4e-4e73-aa6f-9e2da0337f60 | 8 |
056f117a-163e-4cd9-acf1-362837c9904a | 53aa4cf1-99bf-46a0-acd6-11fc5b28e076 | f3f3e46b-8ca8-44cb-a4b5-5a8970f905ba | 2 |
056f117a-163e-4cd9-acf1-362837c9904a | 53aa4cf1-99bf-46a0-acd6-11fc5b28e076 | f719e55b-8142-43a7-a865-29e053d05660 | 24 |
Total | Total | Total | 34 |
071632d3-65ae-4f13-b45d-e158e46bbf24 | E963F8E2-4713-4B7C-A154-CC4ED75A0CAA | 0C00F223-F9B7-4C23-A762-CC8A23D25F2C | 16 |
071632d3-65ae-4f13-b45d-e158e46bbf24 | E963F8E2-4713-4B7C-A154-CC4ED75A0CAA | 596BDF31-0038-4C36-AA95-B03F3D9C7B06 | 48 |
Total | Total | Total | 64 |
四、拓展
【拓展】如果题目改为:
对每个项目做一个WorkHour的合计,如何运用with rollup实现
①调整groupby顺序(调整后:)
GROUP BYProjectID ,HourFiller,ProBatchesID
②调整case when的判空条件(查表后具体问题具体分析)
③调整筛选范围(查表后具体问题具体分析)
【注意】
以下这种情况,虽然数据都被覆盖为Total,但是实际的值还是null,023d991d-e3cb-412b-9226-ec7fca86833c,null,换句话说,SELECT * FROM TABLE WHERE UserID IS NULL 能筛选出最后一条数据