我试图将计数值除以子查询的值并乘以100以获得其百分比。以下代码有效......
SELECT data, rota as rt, COUNT(cliente_id) /
(
SELECT COUNT(DISTINCT cliente_id)
FROM vw_grafico
WHERE rota = rt
) * 100 ) AS porcentagem
FROM vw_grafico
WHERE WEEKDAY(data)+1 = rota
AND YEAR(data) = 2013
GROUP BY month(data)
ORDER BY data ASC
...但是,除此之外,我需要按月计算这个值的平均值,这就是我的问题......当我尝试这样做时,我有错误“无效使用组功能”:
SELECT data, rota as rt, AVG(COUNT(cliente_id) /
(
SELECT COUNT(DISTINCT cliente_id)
FROM vw_grafico
WHERE rota = rt
) * 100 ) AS porcentagem
FROM vw_grafico
WHERE WEEKDAY(data)+1 = rota
AND YEAR(data) = 2013
GROUP BY month(data)
ORDER BY data ASC
我该怎么做才能解决此错误?
答案 0 :(得分:0)
Select list fields( not aggregates) must be Subset of Group by List that in your query this is not true and your error is for that.
在您的查询中:
SELECT data, rota as rt, AVG(COUNT(cliente_id) /
(
SELECT COUNT(DISTINCT cliente_id)
FROM vw_grafico
WHERE rota = rt
) * 100 ) AS porcentagem
FROM vw_grafico
WHERE WEEKDAY(data)+1 = rota
AND YEAR(data) = 2013
GROUP BY month(data)
ORDER BY data ASC
选择列表是:
data, rota as rt
和 小组名单是:
month(data)
选择列表字段不是组列表的子集。所有选择列表字段必须在组列表中,例如:
SELECT month(data), rota as rt, AVG(COUNT(cliente_id) /
(
SELECT COUNT(DISTINCT cliente_id)
FROM vw_grafico
WHERE rota = rt
) * 100 ) AS porcentagem
FROM vw_grafico
WHERE WEEKDAY(data)+1 = rota
AND YEAR(data) = 2013
GROUP BY month(data), rota
ORDER BY data ASC