oracle宝宝出生天数日历

小女今年3月5号出生,朋友时常问起小女多大,每次都要算下天数,很是麻烦,直接用sql语句查询出来,很是方便

select max(d1) 星期日,
       max(d2) 星期一,
       max(d3) 星期二,
       max(d4) 星期三,
       max(d5) 星期四,
       max(d6) 星期五,
       max(d7) 星期六
  from (select decode(d, 1, l || '(' || t || '天)') d1,
               decode(d, 2, l || '(' || t || '天)') d2,
               decode(d, 3, l || '(' || t || '天)') d3,
               decode(d, 4, l || '(' || t || '天)') d4,
               decode(d, 5, l || '(' || t || '天)') d5,
               decode(d, 6, l || '(' || t || '天)') d6,
               decode(d, 7, l || '(' || t || '天)') d7,
               l,
               case
                 when d >= fd then
                  row_number() over(partition by d order by l) - 1
                 else
                  row_number() over(partition by d order by l)
               end ro
          from (select lpad(level, 2, '0') l, --日期
                       to_date(to_char(to_date(to_char(sysdate, 'yyyy-mm') ||
                                               '-01',
                                               'yyyy-mm-dd') + level - 1,
                                       'yyyy-mm-dd') || ' 0:00:00',
                               'yyyy-mm-dd hh24:mi:ss') -
                       to_date('2012-03-05', 'yyyy-mm-dd') + 1 t, --宝宝出生多少天
                       to_char(add_months(last_day(sysdate), -1) + level, 'd') d, --星期几
                       to_char(add_months(last_day(sysdate), -1) + 1, 'd') fd --第一天星期几
                  from dual
                connect by level <= to_char(last_day(sysdate), 'dd')))
 group by ro

 order by nvl(max(d1), 0)

 

你可能感兴趣的:(oracle)