一条sql统计出个阶段的数量
程序员文章站
2022-05-09 15:14:33
...
需求案例:统计出各年龄段人数
表结构:
sql:
SELECT
count( a1 ) AS A1,
count( a2 ) AS A2,
count( a3 ) AS A3
FROM
(
SELECT
( CASE WHEN age <= 18 THEN age END ) AS a1,
( CASE WHEN age > 18 AND age <= 22 THEN age END ) AS a2,
( CASE WHEN age > 22 THEN age END ) AS a3
FROM
sys_user
) t
结果: