对于一个项目,我有一个汇总数据的查询:
SELECT
code,
SUM(someValue) as summedValueForCode
FROM
someTable
GROUP BY
code;
现在,我需要的是第三列,它定义 summedValueForCode 代表所有行的百分比。所以 summedValueForCode / summedValueForAllRows。
我可以做类似的事情:
SELECT
code,
SUM(someValue) as summedValueForCode,
(100 * SUM(someValue) / (SELECT sum(someValue) FROM someTable)) as valueForCodeToTotal
FROM
someTable
GROUP BY
code;
但它看起来不是很优雅。
另一种选择是:
SELECT
code,
SUM(someValue) as summedValueForCode,
(100 * SUM(someValue) / totalSum) as valueForCodeToTotal
FROM
someTable,
(SELECT sum(someValue) as totalSum FROM someTable) as summedTable
GROUP BY
code;
但是,由于真正的查询非常复杂,我不喜欢第二次添加这样的查询的想法。
有没有办法使用聚合函数来计算忽略 GROUP 的所有行的 SUM?