常用的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;