一個項目,我有一個查詢,我彙總數據:MySQL的:每組總和與總
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?
我擔心的一樣多。感謝您的確認! – Jeroen