我试图找到pageType的百分比评级小于20。
我的表格如下:
requestId rank pageType
MMCVS 0 HOME
MMCVS 1 MOBILE
MMCVS 2 HOME
BBVSS 0 HOME
BBVSS 11 MOBILE
BBVSS 12 HOME
到目前为止,我试过了:
select pageType, Top20, Top20/count(DISTINCT requestId) as Percentage
from (
SELECT
pageType, requestId,
SUM(CASE WHEN rank <= 20 THEN 1 ELSE 0 END) as Top20
FROM
tempTable
group by pageType, requestId) tempTable group by pageType
但是得到错误:
expression 'Top20' is neither present in the group by, nor is it an aggregate function.
答案 0 :(得分:0)
是的,您需要将任何非聚合字段添加到逐个字段列表中。虽然MySQL不会给你一个错误,即使你没有。
就此而言,您要么将Top20添加到group by中,要么从select中删除Top20:
SELECT pageType, Top20/count(DISTINCT requestId) as Percentage
FROM (
SELECT pageType, requestId,
SUM(CASE WHEN rank <= 20 THEN 1 ELSE 0 END) as Top20
FROM tempTable
GROUP BY pageType, requestId
) tempTable
GROUP BY pageType