oracle中根据“”生日’字段查询数据的一些sql语句

本文介绍在Oracle数据库中如何使用SQL查询个人年龄,统计特定年龄段的人员数量,以及筛选指定年龄段的所有记录。通过案例演示,帮助读者掌握利用SQL进行年龄数据分析的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

常用的sql
一:在oracle数据表中只有“生日”字段,根据生日查询年龄:

select trunc(months_between(sysdate,birth_date)/12) from Table(Table为表名);

二:根据“生日”字段统计时间段内的人员数据:(思路;符合条件为一,否则为零,然后相加统计)

select sum(case
when to_number(to_char(sysdate,‘yyyy’)-to_char(sysdate,‘yyyy’)) 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’
from Table(Table为表名);

三:根据生日字段查询年龄段之间的所有数据:

select * from (
select t.* trunc(months_between(sysdate,birth_date)/12) as age from Table t(Table为表名)
) n where n.age>15 and n.age<30;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值