A. sql中只有出生年月怎么按年龄段来统计
假如table中有older这个字段存放出生年月的
select (datepart(year,getdate())-datepart(year,older))as '年龄' from table
即可实现该功能!
B. 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性别
C. 利用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
D. 如何写一个sql语句能根据出生日期按年龄段统计人数
你需要使用两种SQL语句:
COUNT( )
BETWEEN
然后你的资料表要有纪录年龄,或是至少要有出生日期。
大概的语法如下:
SELECT COUNT( 栏位名 ) FROM 资料表 WHERE 年龄 BETWEEN '40' and '50'
栏位名:不能是可以允许NULL值的栏位,有NULL的资料不会计算在内。
资料表:你要抓资料的那个资料表。
BETWEEN '40' and '50':意思是介于40~50岁。
E. sql出生年月统计年龄段
SELECT
SUM(CASEWHENyears>=20ANDyears<30THEN1ELSE0END)AS'20-30岁',
SUM(CASEWHENyears>=30ANDyears<40THEN1ELSE0END)AS'30-40岁',
SUM(CASEWHENyears>=40ANDyears<50THEN1ELSE0END)AS'40-50岁'FROM
(SELECTDATEDIFF(YEAR,Birthday,GETDATE())ASyearsFROMdbo.c_driver)ABC
F. 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 表
G. SQL统计年龄段
例如:
select case when [年龄] BETWEEN 10 AND 20 then '10-20'
when [年龄] BETWEEN 20 AND 30 then '20-30'
when [年龄] > 30 then '30以上' end as '年龄段',
count(*) as '人数' FROM [Table]
H. 用sql语句实现年龄分段统计
先将年龄除10取整
select floor(年龄/10) as age from 表
再根据年龄整数分组统计
select age ,count(age) from
(
select floor(年龄/10) as age from 表
)
group by age
这样基本效果就出来了,达到楼主的要求就要加如函数计算了
sql语法
select convert(varchar,age*10)+'--'+convert(varchar,(age+1)*10) ,count(age) from
(
select floor(年龄/10) as age from 表
)
group by age
oracle语法
select age*10 || '--'|| (age+1)*10 ,count(age) from
(
select floor(年龄/10) as age from 表
)
group by age
I. 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
J. 用SQL语句查询年龄分段的分组查询 我的数据表格式如下
--不知道你要的是不是这样的结果
CreatetableT
(
籍贯varchar(10),性别varchar(2),生日varchar(10)
)
insertintotvalues('四川','男','19890627')
insertintotvalues('新疆','男','19930711')
insertintotvalues('河南','男','19890623')
insertintotvalues('四川','女','19880512')
insertintotvalues('新疆','男','19950425')
insertintotvalues('河南','女','19800304')
Select籍贯,性别,
Sum(CaseWhen年龄<=20Then1Else0End)As[0-20岁(人)],
Sum(CaseWhen年龄Between21And40Then1Else0End)As[21-40岁(人)],
Sum(CaseWhen年龄Between41And60Then1Else0End)As[41-60岁(人)],
Sum(CaseWhen年龄>=61Then1Else0End)As[60岁以上(人)]
From
(
Select*,Year(GETDATE())-Year(convert(Datetime,生日))As年龄
Fromt
)s
GroupBy籍贯,性别
withrollup