我当前的查询:
SELECT Delaytype, SUM('Delayhours') as cnt
FROM delays
GROUP BY Delaytype;
如何将cnt
作为百分比而不是总出现次数的总和?
答案 0 :(得分:1)
我想这就是你想要的:
SELECT Delaytype, SUM(Delayhours) / (SELECT SUM(Delayhours) FROM delays) * 100 as cntper
FROM delays
GROUP BY Delaytype
或者,如果您的数据库支持子选择,这应该更有效:
SELECT Delaytype, cnt / SUM(cnt) * 100 FROM (
SELECT Delaytype, SUM(Delayhours) as cnt
FROM delays
GROUP BY Delaytype
) AS GroupedDelays
答案 1 :(得分:0)
一种方法是交叉加入总和:
SELECT d.Delaytype, SUM(d.Delayhours)/total_delay as pct
FROM delays
CROSS JOIN (SELECT SUM(Delayhours) AS total_delay
FROM delays) t
GROUP BY d.Delaytype;