select sum(case
when datediff(year,Birthday,getdate()) between 21 and 30
then 1
else 0
end
) as '21-30'
,sum(case
when datediff(year,Birthday,getdate()) between 31 and 40
then 1
else 0
end
) as '31-40'
,sum(case
when datediff(year,Birthday,getdate()) between 41 and 50
then 1
else 0
end
) as '41-50'
when datediff(year,Birthday,getdate()) between 21 and 30
then 1
else 0
end
) as '21-30'
,sum(case
when datediff(year,Birthday,getdate()) between 31 and 40
then 1
else 0
end
) as '31-40'
,sum(case
when datediff(year,Birthday,getdate()) between 41 and 50
then 1
else 0
end
) as '41-50'
本文介绍了一种使用 SQL 语句来统计不同年龄区间的人数的方法。通过 CASE WHEN 语句结合 DATEDIFF 函数,可以有效地计算出每个年龄段的人员数量。

被折叠的 条评论
为什么被折叠?



