CREATE TABLE t_user3(
id NUMBER PRIMARY KEY,
user_name VARCHAR2(20),
birt_date date -->>java.util.Date(包含日期+时间)
)
INSERT INTO t_user3 VALUES (1,'小明',to_date('1995-10-15','yyyy-MM-dd'));
INSERT INTO t_user3 VALUES (2,'小黄',to_date('1985-09-05','yyyy-MM-dd'));
INSERT INTO t_user3 VALUES (3,'小军',to_date('1987-07-05','yyyy-MM-dd'));
INSERT INTO t_user3 VALUES (4,'大林',to_date('1967-09-15','yyyy-MM-dd'));
INSERT INTO t_user3 VALUES (5,'彤彤',to_date('2003-09-15','yyyy-MM-dd'));
commit
select * from t_user3
action
service -->>遍历 userList (age)
--java的日历类有强大的时间求算能力
dao -->>不对age
entity
User
int id;
String userName;
java.util.Date birtDate;
int age;
-->>对于不在java中的程序,如果要算日期
--->>只能在sql运算
-- 1.算年龄,增加多1个字段
select
id,
user_name,
-- birt_date,
trunc(months_between(sysdate,birt_date)/12) age
from
t_user3
-- 1.看单一值 (可以使用decode去取代)
-- 2.看范围
case when
-- 2.再增加一个年龄分层
select
id,
user_name,
age,
case
when age<19 then '未成年人'
when age<25 then '年青人'
when age<40 then '中青'
when age<50 then '中年人'
when age<60 then '中老年人'
else '老人'
end ageLevel
from (
select
id,
user_name,
-- birt_date,
trunc(months_between(sysdate,birt_date)/12) age
from
t_user3
) t_user3_age
--3.对年龄分层进行统计
select agelevel,count(1) kk from (
select
id,
user_name,
age,
case
when age<19 then '未成年人'
when age<25 then '年青人'
when age<40 then '中青'
when age<50 then '中年人'
when age<60 then '中老年人'
else '老人'
end ageLevel
from (
select
id,
user_name,
-- birt_date,
trunc(months_between(sysdate,birt_date)/12) age
from
t_user3
) t_user3_age
) t_user3_age_level
group by agelevel
order by kk desc
select * from t_user3