WITH ROLLUP 只产生 NULL

WITH ROLLUP is only producing NULLs

这就是 table 的样子 "normally"

WorkloadCategory | WorkloadCapacity| WorkloadTotalTime
-----------------|-----------------|------------------
DI               | 317632          | 239.92
DI               | 106706          | 32.45
DI               | 35840           | 27.77
DI               | 50000           | 48.07
DI               | 8000            | 9.18
DI               | 29120           | 15.71
DI               | 0               | 0

使用以下查询:

SELECT
    wlc.WorkloadCategory,
    wl.WorkloadCapacity,
    ROUND(wl.WorkloadMinutes * wl.WorkloadCapacity / 60 / assum.WorkYearHours, 2) AS WorkloadTotalTime
FROM
    swam.Assumptions assum
CROSS JOIN 
    swam.WorkloadCategories wlc
INNER JOIN 
    swam.Workloads wl ON wlc.WorkloadCategoryID = wl.WorkloadCategoryID
ORDER BY
    wlc.WorkloadCategory

我想做的是简单地对 WorkloadCapacityWorkloadTotalTime 两列求和,并在 table 的底部有一个新行,显示每一列的总和。

我希望某些列为 NULL,因为这就是 ROLLUP 的工作方式,是的,我知道我需要指定列名才能读取“总计”……但我不明白的是为什么 ROLLUP 的总和根本没有显示出来。

我尝试使用此站点上另一个 post 的 GROUP BY GROUPING SET(或类似的东西)进行类似操作,但它没有解决我遇到的问题。

SELECT
    wlc.WorkloadCategory,
    ROUND(wl.WorkloadMinutes * wl.WorkloadCapacity / 60 / assum.WorkYearHours, 2) AS WorkloadTotalTime
FROM
    swam.Assumptions assum
CROSS JOIN 
    swam.WorkloadCategories  wlc
INNER JOIN 
    swam.Workloads wl ON wlc.WorkloadCategoryID = wl.WorkloadCategoryID
GROUP BY
    wlc.WorkloadCategory, wl.WorkloadCapacity, 
    assum.WorkYearHours, wl.WorkloadMinutes WITH ROLLUP

输出 table 完全是 NULLS!

WorkloadCategory | WorkloadCapacity | WorkloadTotalTime
DI               | 0                | 0
DI               | 0                | NULL
DI               | 0                | NULL
DI               | 8000             | 9.18
DI               | 8000             | NULL
DI               | 8000             | NULL
DI               | 29120            | 15.71
DI               | 29120            | NULL
DI               | 29120            | NULL
DI               | 35840            | 27.77
DI               | 35840            | NULL
DI               | 35840            | NULL
DI               | 50000            | 48.07
DI               | 50000            | NULL
DI               | 50000            | NULL
DI               | 106706           | 32.45
DI               | 106706           | NULL
DI               | 106706           | NULL
DI               | 317632           | 239.92
DI               | 317632           | NULL
DI               | 317632           | NULL
DI               | NULL             | NULL

在此先感谢您提供的任何帮助。

您的 WITH ROLLUP 不起作用,因为您已告诉查询在每个数字字段上进行 GROUP 作为类别,但您没有告诉它如何对总数求和。此外,WITH ROLLUP 将在 4 个字段中的每个字段中汇总,但这不是您想要的。最后你只需要一个总数(可能是每个类别的总数)所以使用 GROUPING SETS 你可以告诉它你想要什么。

像这样:

SELECT
    wlc.WorkloadCategory,
    SUM(wl.WorkloadCapacity) AS WorkloadCapacity,
    SUM(ROUND(wl.WorkloadMinutes * wl.WorkloadCapacity / 60 / assum.WorkYearHours, 2)) AS WorkloadTotalTime
FROM
    swam.Assumptions assum
CROSS JOIN 
    swam.WorkloadCategories  wlc
INNER JOIN 
    swam.Workloads wl ON wlc.WorkloadCategoryID = wl.WorkloadCategoryID
GROUP BY GROUPING SETS (
  (wlc.WorkloadCategory, wl.WorkloadCapacity, assum.WorkYearHours, wl.WorkloadMinutes),
  (wlc.WorkloadCategory),
  ()
)