利用SQL语句统计出各年龄段人数

2025-04-27 09:11:36
推荐回答(2个)
回答1:

select '25-30岁' as 年龄段 count(*) as 人数 from tb where year(getdate())-year(birthday)>=25 and year(getdate())-year(birthday)<30
union all
select '30-35岁' as 年龄段 count(*) as 人数 from tb where year(getdate())-year(birthday)>=30 and year(getdate())-year(birthday)<35
union all
select '35-40岁' as 年龄段 count(*) as 人数 from tb where year(getdate())-year(birthday)>=35 and year(getdate())-year(birthday)<40

回答2:

select count(*) as 25-30岁 from table where data>1985-10-12 and date<...
union all
select count(*) as