zoukankan      html  css  js  c++  java
  • Oracle-计算岁数

    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

  • 相关阅读:
    【译】NodeJS and Good Practices
    【译】单一责任原则
    CSS 属性 z-index
    Node 连接 MySql
    CentOS 7 后台克隆远程库
    H5log打印
    利用Promise实现Promise.all
    Set、Map、WeakSet、WeakMap 理解与使用
    vue如何禁止弹窗后面的滚动条滚动?
    vue面试题总结
  • 原文地址:https://www.cnblogs.com/sheying/p/8578548.html
Copyright © 2011-2022 走看看