计算MYSQL组中的百分比

时间:2013-07-31 10:31:22

标签: mysql

我的查询按优先级计算SLA内的呼叫总数。但是现在我需要在同一个语句中按优先级计算出SLA中的调用百分比

但是我不确定如何在我当前的陈述中做到这一点

`SELECT
 SUM(o.withinfix) 'Total'
 ,o.priority 'priority'
 FROM opencall o
 WHERE LEFT(FROM_UNIXTIME(o.logdatex),4) = "2013"
 AND o.fk_company_id = "abc"
 GROUP BY o.priority'

任何人都可以帮忙,我使用的是MYSQL 5.1

由于

2 个答案:

答案 0 :(得分:1)

 SELECT SUM(o.withinfix) 'Total',
        SUM(o.withinfix) / (select SUM(withinfix) from opencall) * 100 as perc
        o.priority 'priority'
 FROM opencall o
 WHERE LEFT(FROM_UNIXTIME(o.logdatex),4) = "2013"
 AND o.fk_company_id = "abc"
 GROUP BY o.priority

答案 1 :(得分:0)

您只需选择总计:

即可
SELECT SUM(o.withinfix) 'Total' ,o.priority 'priority',
    (SELECT SUM(withinfix) FROM opencall AS o2 GROUP BY o2.priority) AS sum_total
FROM opencall o
WHERE LEFT(FROM_UNIXTIME(o.logdatex),4) = "2013"
AND o.fk_company_id = "abc" GROUP BY o.priority'

查询后手动计算percantage。