题解 | 计算25岁以上和以下的用户数量
计算25岁以上和以下的用户数量
https://www.nowcoder.com/practice/30f9f470390a4a8a8dd3b8e1f8c7a9fa
-- 虽然 GROUP BY 逻辑上先于 SELECT 执行,但 SQL 语法支持在 GROUP BY 中直接引用 SELECT 定义的别名,这是语法解析器的 “智能替换” 机制,既保证了执行逻辑的正确性,又提升了 SQL 的可读性。
select
case when age < 25 or age is null then '25岁以下' else '25岁及以上' end age_cut,
count(*) number
from
user_profile
group by
age_cut
order by
case when age_cut = '25岁以下' then 1 else 2 end;

