这就是表格的样子"通常"
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
我想要做的只是将两列WorkloadCapacity
和WorkloadTotalTime
相加,并在表格的底部有一个新行,显示每列的总和。
我希望某些列为NULL,因为这是ROLLUP的工作方式,是的,我知道我需要指定列名来读取' Total"如果我想......但我不明白为什么ROLLUP的总和甚至都没有出现。
我在本网站的其他帖子中使用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
输出表只是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
提前感谢您提供的任何帮助。
答案 0 :(得分:1)
您的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),
()
)