实现按年龄统计的一个问题

bsten 2003-10-09 10:34:44
数据库中,用varchar2存的生日,格式是2003.10.1。现在要按年龄进行统计,比如
3岁一下的,3-5岁的,5岁以上的。请问应该如何处理?
...全文
101 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
ganjian 2003-10-09
  • 打赏
  • 举报
回复
如下可以
select '3岁以下',count(*) 年龄 from babytable where ceil((sysdate-to_date(birthday,'yyyy-mm-dd'))/365)<=3
union
select '3-5岁',count(*) 年龄 from babytable where ceil((sysdate-to_date(birthday,'yyyy-mm-dd'))/365)>3
and ceil((sysdate-to_date(birthday,'yyyy-mm-dd'))/365)<=5
union
select '5岁以上',count(*) 年龄 from babytable where ceil((sysdate-to_date(birthday,'yyyy-mm-dd'))/365)>3
and ceil((sysdate-to_date(birthday,'yyyy-mm-dd'))/365)>5

Nickle_Final 2003-10-09
  • 打赏
  • 举报
回复
sorry,
应该这样:
select * from your_table where to_char(sysdate,'yyyy')-substr(your_column,1,4) <=your_age
或者
select * from your_table where to_char(sysdate,'yyyy')-substr(your_column,1,4) >=your_age
或者
select * from your_table where to_char(sysdate,'yyyy')-substr(your_column,1,4) between your_age1 and your_age2

Nickle_Final 2003-10-09
  • 打赏
  • 举报
回复
酱紫应该可以了
select * from your_table where to_char(sysdate,'yyyy')-substr(your_column,1,4)=your_number
anne 2003-10-09
  • 打赏
  • 举报
回复
建表:Create table ss(a varchar2(20), b number);
插入的数据:
A B
-------------------- ----------
2003.10.1 1
2000.10,10 2
1999.1.1 3
1977.1.1 4
查询语句:
select a,
case
when to_char(sysdate,'yyyy')-substr(a,1,4) in ('0','1','2','3') then '3岁以下'
when to_char(sysdate,'yyyy')-substr(a,1,4) in ('3','4','5') then '3-5岁'
else '5岁以上'
end as 年龄段,
b
from ss;
结果:
A 年龄段 B
-------------------- ------- ----------
2003.10.1 3岁以下 1
2000.10,10 3岁以下 2
1999.1.1 3-5岁 3
1977.1.1 5岁以上 4
不知是不是你想要的结果?

17,078

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧