问题: 为什么我无法在
GROUP BY num_accounts 中
select c.cust_id, COUNT(*) num_accounts
from customer c LEFT JOIN
account a on a.account_id = c.cust_id
GROUP BY c.cust_id, num_accounts;
相反,我必须将' num_accounts '放入 ORDER BY 子句:
select c.cust_id, COUNT(*) num_accounts
from customer c LEFT JOIN
account a on a.account_id = c.cust_id
GROUP BY c.cust_id
ORDER BY num_accounts;
?
我的假设是 num_accounts 是由聚合生成的列,应使用GROUP BY。
谢谢
答案 0 :(得分:0)
此SQL的处理顺序与写入顺序不同。首先加入,在哪里,分组,选择,然后最后排序。这意味着分阶段不存在num_accounts。您可以改用它
GROUP BY c.cust_id, COUNT(*)