C#高级:Lambda表达式分组处理2(WITH ROLLUP关键字)

2024-01-03 17:23:58

目录

一、问题引入

二、with rollup查询

三、去掉多余数据

四、拓展


一、问题引入

查询SQL后结果如下,字段分别是用户、项目、批次、工作时间:

SELECT UserID,ProjectID,ProBatchesID,WorkHour?
FROM ?MAINTABLE
GROUP BY
HourFiller
,ProjectID?
,ProBatchesID?

结果:

UserIDProjectIDProBatchesIDWorkHour
0400bb0e-a957-479e-98e0-60c4542eefa6E963F8E2-4713-4B7C-A154-CC4ED75A0CAA0C00F223-F9B7-4C23-A762-CC8A23D25F2C72
0400bb0e-a957-479e-98e0-60c4542eefa6E963F8E2-4713-4B7C-A154-CC4ED75A0CAA596BDF31-0038-4C36-AA95-B03F3D9C7B06224
052a3c6e-3a0e-44d2-8d5c-177760d03768E963F8E2-4713-4B7C-A154-CC4ED75A0CAA0C00F223-F9B7-4C23-A762-CC8A23D25F2C72
052a3c6e-3a0e-44d2-8d5c-177760d03768E963F8E2-4713-4B7C-A154-CC4ED75A0CAA596BDF31-0038-4C36-AA95-B03F3D9C7B06224
056f117a-163e-4cd9-acf1-362837c9904a53aa4cf1-99bf-46a0-acd6-11fc5b28e0761accce0a-1d4e-4e73-aa6f-9e2da0337f608
056f117a-163e-4cd9-acf1-362837c9904a53aa4cf1-99bf-46a0-acd6-11fc5b28e076f3f3e46b-8ca8-44cb-a4b5-5a8970f905ba2
056f117a-163e-4cd9-acf1-362837c9904a53aa4cf1-99bf-46a0-acd6-11fc5b28e076f719e55b-8142-43a7-a865-29e053d0566024
071632d3-65ae-4f13-b45d-e158e46bbf24E963F8E2-4713-4B7C-A154-CC4ED75A0CAA0C00F223-F9B7-4C23-A762-CC8A23D25F2C16
071632d3-65ae-4f13-b45d-e158e46bbf24E963F8E2-4713-4B7C-A154-CC4ED75A0CAA596BDF31-0038-4C36-AA95-B03F3D9C7B0648

我要的需求是:对每个人每个项目做一个WorkHour的合计,如何运用with rollup实现

二、with rollup查询

WITH TEMP AS(
?? ?SELECT UserID,ProjectID,ProBatchesID,WorkHour FROM ?MAINTABLE?
?? ?GROUP BY
?? ?HourFiller
?? ?,ProjectID?
?? ?,ProBatchesID?
?? ?WITH ROLLUP
)
SELECT * FROM TEMP

结果:

UserIDProjectIDProBatchesIDWorkHour
0400bb0e-a957-479e-98e0-60c4542eefa6E963F8E2-4713-4B7C-A154-CC4ED75A0CAA0C00F223-F9B7-4C23-A762-CC8A23D25F2C72
0400bb0e-a957-479e-98e0-60c4542eefa6E963F8E2-4713-4B7C-A154-CC4ED75A0CAA596BDF31-0038-4C36-AA95-B03F3D9C7B06224
0400bb0e-a957-479e-98e0-60c4542eefa6E963F8E2-4713-4B7C-A154-CC4ED75A0CAANULL296
0400bb0e-a957-479e-98e0-60c4542eefa6NULLNULL296
052a3c6e-3a0e-44d2-8d5c-177760d03768E963F8E2-4713-4B7C-A154-CC4ED75A0CAA0C00F223-F9B7-4C23-A762-CC8A23D25F2C72
052a3c6e-3a0e-44d2-8d5c-177760d03768E963F8E2-4713-4B7C-A154-CC4ED75A0CAA596BDF31-0038-4C36-AA95-B03F3D9C7B06224
052a3c6e-3a0e-44d2-8d5c-177760d03768E963F8E2-4713-4B7C-A154-CC4ED75A0CAANULL296
052a3c6e-3a0e-44d2-8d5c-177760d03768NULLNULL296
056f117a-163e-4cd9-acf1-362837c9904a53aa4cf1-99bf-46a0-acd6-11fc5b28e0761accce0a-1d4e-4e73-aa6f-9e2da0337f608
056f117a-163e-4cd9-acf1-362837c9904a53aa4cf1-99bf-46a0-acd6-11fc5b28e076f3f3e46b-8ca8-44cb-a4b5-5a8970f905ba2
056f117a-163e-4cd9-acf1-362837c9904a53aa4cf1-99bf-46a0-acd6-11fc5b28e076f719e55b-8142-43a7-a865-29e053d0566024
056f117a-163e-4cd9-acf1-362837c9904a53aa4cf1-99bf-46a0-acd6-11fc5b28e076NULL34
056f117a-163e-4cd9-acf1-362837c9904aNULLNULL34
071632d3-65ae-4f13-b45d-e158e46bbf24E963F8E2-4713-4B7C-A154-CC4ED75A0CAA0C00F223-F9B7-4C23-A762-CC8A23D25F2C16
071632d3-65ae-4f13-b45d-e158e46bbf24E963F8E2-4713-4B7C-A154-CC4ED75A0CAA596BDF31-0038-4C36-AA95-B03F3D9C7B0648
071632d3-65ae-4f13-b45d-e158e46bbf24E963F8E2-4713-4B7C-A154-CC4ED75A0CAANULL64
071632d3-65ae-4f13-b45d-e158e46bbf24NULLNULL64

发现结果中不同人不同项目会自动分组合计,因此我们去掉多余的合计数据即可。

三、去掉多余数据

WITH TEMP AS(
?? ?SELECT UserID,ProjectID,ProBatchesID,WorkHour FROM ?MAINTABLE?
?? ?GROUP BY
?? ?HourFiller
?? ?,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 /*去掉多余的合计数据*/?

结果:

UserIDProjectIDProBatchesIDWorkHour
0400bb0e-a957-479e-98e0-60c4542eefa6E963F8E2-4713-4B7C-A154-CC4ED75A0CAA0C00F223-F9B7-4C23-A762-CC8A23D25F2C72
0400bb0e-a957-479e-98e0-60c4542eefa6E963F8E2-4713-4B7C-A154-CC4ED75A0CAA596BDF31-0038-4C36-AA95-B03F3D9C7B06224
TotalTotalTotal296
052a3c6e-3a0e-44d2-8d5c-177760d03768E963F8E2-4713-4B7C-A154-CC4ED75A0CAA0C00F223-F9B7-4C23-A762-CC8A23D25F2C72
052a3c6e-3a0e-44d2-8d5c-177760d03768E963F8E2-4713-4B7C-A154-CC4ED75A0CAA596BDF31-0038-4C36-AA95-B03F3D9C7B06224
TotalTotalTotal296
056f117a-163e-4cd9-acf1-362837c9904a53aa4cf1-99bf-46a0-acd6-11fc5b28e0761accce0a-1d4e-4e73-aa6f-9e2da0337f608
056f117a-163e-4cd9-acf1-362837c9904a53aa4cf1-99bf-46a0-acd6-11fc5b28e076f3f3e46b-8ca8-44cb-a4b5-5a8970f905ba2
056f117a-163e-4cd9-acf1-362837c9904a53aa4cf1-99bf-46a0-acd6-11fc5b28e076f719e55b-8142-43a7-a865-29e053d0566024
TotalTotalTotal34
071632d3-65ae-4f13-b45d-e158e46bbf24E963F8E2-4713-4B7C-A154-CC4ED75A0CAA0C00F223-F9B7-4C23-A762-CC8A23D25F2C16
071632d3-65ae-4f13-b45d-e158e46bbf24E963F8E2-4713-4B7C-A154-CC4ED75A0CAA596BDF31-0038-4C36-AA95-B03F3D9C7B0648
TotalTotalTotal64

四、拓展

?【拓展】如果题目改为:
对每个项目做一个WorkHour的合计,如何运用with rollup实现
①调整groupby顺序(调整后:)

?? ?GROUP BY
?? ? ProjectID?
?? ?,HourFiller
?? ?,ProBatchesID?

②调整case when的判空条件(查表后具体问题具体分析)
③调整筛选范围(查表后具体问题具体分析)

【注意】
以下这种情况,虽然数据都被覆盖为Total,但是实际的值还是null,023d991d-e3cb-412b-9226-ec7fca86833c,null,换句话说,SELECT * FROM TABLE WHERE UserID IS NULL 能筛选出最后一条数据

文章来源:https://blog.csdn.net/m0_67412019/article/details/135365743
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。