当前位置:首页 » 编程语言 » sql如何计算某个年龄段的人数
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

sql如何计算某个年龄段的人数

发布时间: 2022-06-22 01:05:09

sql语句 根据年纪阶段统计人数 根据性别分组

select性别,
casewhen年龄between20and29then1else0end[20-29],
casewhen年龄between30and39then1else0end[30-39],
casewhen年龄between40and49then1else0end[40-49]
from表名
groupby性别

以上使用于大部分数据库

只是在起别名上,只适用于sqlserver

oracle的话起别名

select性别,
casewhen年龄between20and29then1else0end"20-29",
casewhen年龄between30and39then1else0end"30-39",
casewhen年龄between40and49then1else0end"40-49"
from表名
groupby性别

mysql的话

select性别,
casewhen年龄between20and29then1else0end`20-29,
casewhen年龄between30and39then1else0end`30-39`,
casewhen年龄between40and49then1else0end`40-49
from表名
groupby性别

❷ 如何写一个sql语句能根据出生日期按年龄段统计人数

你需要使用两种SQL语句:

  • COUNT( )

  • BETWEEN

然后你的资料表要有纪录年龄,或是至少要有出生日期。

大概的语法如下:

SELECT COUNT( 栏位名 ) FROM 资料表 WHERE 年龄 BETWEEN '40' and '50'

栏位名:不能是可以允许NULL值的栏位,有NULL的资料不会计算在内。

资料表:你要抓资料的那个资料表。

BETWEEN '40' and '50':意思是介于40~50岁。

❸ sql 查询每个年龄的人数

select COUNT(*) as 每个年龄的人数,年龄 from 员工表 group by 年龄

❹ SQL 只有出生日期 如何统计出相同年龄人数 给我具体语句

假如统计1990-5-5相同年龄的人,主键为ID,生日的字段为Birthday SQL语句: SELECT COUNT(ID) FROM 表名 WHERE Birthday BETWEEN '1990-1-1 00:00:00' AND '1990-12-31:23:59:59'

麻烦采纳,谢谢!

❺ sql语句 怎么统计各年龄段人数分布情况 年龄为user_age,表为worker,年龄已知为数字类型

selectuser_age年龄,count(user_age)人数,cast(cast((count(user_age)/((selectcount(*)fromworker)*1.0)*100)asdecimal(9,2))asvarchar)+'%'所占比例fromworkergroupbyuser_age


❻ SQL统计年龄大于30岁的学生的人数

SQL统计年龄大于30岁的学生的人数:
select count(*) as 人数
from student
where(year(gatdate())-year(birthday))>30

❼ SQL语句按年龄分组,统计各个年龄的人数

先确保你的出生年月是datetime的日期类型,语法如下。

select case when datediff(year,出生年月,getdate()) <= 20 then '20岁年龄段'
when datediff(year,出生年月,getdate()) between 21 and 25 then '21-25年龄段'
else '25以上年龄段' end as 年龄段,count(1) as 年龄段人数
from 表
group by
case when datediff(year,出生年月,getdate()) <= 20 then '20岁年龄段'
when datediff(year,出生年月,getdate()) between 21 and 25 then '21-25年龄段'
else '25以上年龄段' end

也可以试试

select sum(case when datediff(year,出生年月,getdate()) <= 20 then 1 else 0 end) '20岁年龄段',
sum(case when datediff(year,出生年月,getdate()) between 21 and 25 then 1 else 0 end) '21-25年龄段',
sum(case when datediff(year,出生年月,getdate()) > 25 then 1 else 0 end) '25以上年龄段'
from 表

❽ SQL语句 按年龄段分组统计人数问题

--很简单啊,楼主请看:
--以下在SQL2005测试通过。
create
table
#t(Uname
varchar(10),age
int)
insert
#t
select
'啊啊',19
union
all
select
'信息',23
union
all
select
'宝宝',31
union
all
select
'喔喔',21
union
all
select
'米米',6
select
nnd
as
'年龄段',count(*)
as
'人数'
from
(
select
case
when
age>=1
and
age<=10
then
'1-10'
when
age>=11
and
age<=20
then
'11-20'
when
age>=21
and
age<=30
then
'21-30'
when
age>=31
and
age<=40
then
'31-40'
end
as
nnd,uname
from
#t
)
a
group
by
nnd

❾ SQL 只有出生日期 如何统计出相同年龄人数

例:表 tab1 ,出生日期字段为 rq,sql语句如下

selecta.age,count(*)from
(selectyear(getdate())-year(rq)as"age"fromtab1)a
groupbya.age;

关键语句:select year(getdate()) - year(rq) as "age" from tab1

year函数获取系统日期与出生日期的年份,将年份相减即年龄,当然也可以根据需求,取出月份,进行运算,获得更精确的周岁。

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

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