我试图通过渠道统计用户,只需要3行的回报。但似乎结果并不像实际数字那样聚合。有人知道为什么我不能同时使用'group by‘和'limit’吗?
select count(users) as cnt
from user_table
group by channel
limit 3
;
/*
channel cnt
a 39
b 27
c 16
*/select count(users) as cnt
from user_table
where channel = 'a'
;
/*
channel cnt
a 2057
*/为什么这两个查询有不同的结果?
发布于 2021-07-02 00:00:24
不确定为什么对相同的组显示不同的计数...也许您的示例过于简单,但是为了使结果具有确定性,应该将LIMIT与order by一起使用
例如:
select count(users) as cnt
from user_table
group by channel
order by cnt desc --top counts first for example
limit 3https://stackoverflow.com/questions/68213313
复制相似问题