zoukankan      html  css  js  c++  java
  • Oracle 中的周、月、日历的查询实现

    /**
    1. 周
    一年总共只有53周
    有两种情况,在于当年第一周的第一天和最后一天怎么算,后面只需加上7天就是一周,类推
    a. 第一种,周日为第一天 ,周六为最后一天
    b. 第二种,周一为第一天,周日为最后一天
    */
    -- a. 第一种
    SELECT LEVEL 周次,
           (Trunc(SYSDATE, 'yyyy') - 7) + (7 - To_Char(Trunc(SYSDATE, 'yyyy'), 'd') + 1) +
           (LEVEL - 1) * 7 当周第一天,
           (Trunc(SYSDATE, 'yyyy') - 7) + (7 - To_Char(Trunc(SYSDATE, 'yyyy'), 'd') + 1) +
           (LEVEL - 1) * 7 + 6 当周最后一天
      FROM dual
    CONNECT BY LEVEL <= 53;
    
    
    -- b. 第二种
    select '' || level || '' as week_no,
           next_day(trunc(sysdate, 'yyyy'), '星期一') + (level - 1) * 7 as monday,
           next_day(trunc(sysdate, 'yyyy'), '星期日') + (level - 1) * 7 as sunday
      from dual
    connect by level <= 53;
    
    
    
    /**
    2、月份
    查询当年每个月的起止时间
    */
    select aa as monNum,
           to_date(mon, 'yyyymmdd') as firstDay,
           LAST_DAY(to_date(mon, 'yyyymmdd')) as lastDay
      from (select to_char(sysdate, 'yyyy') || to_char(aa, '09') || '01' mon, aa
              from (select 1 aa
                      from dual
                    union
                    select 2 aa
                      from dual
                    union
                    select 3 aa
                      from dual
                    union
                    select 4 aa
                      from dual
                    union
                    select 5 aa
                      from dual
                    union
                    select 6 aa
                      from dual
                    union
                    select 7 aa
                      from dual
                    union
                    select 8 aa
                      from dual
                    union
                    select 9 aa
                      from dual
                    union
                    select 10 aa
                      from dual
                    union
                    select 11 aa
                      from dual
                    union
                    select 12 aa from dual));
    
    
    
    /**
    3、日历
    以下查询可输入年份
    */
    select case
             when (new_yweek = min(new_yweek) over(partition by mon order by new_yweek)) then
              mon_name
             else
              null
           end as month,  -- 月份
           new_yweek as yweek,  -- 一年中的第几周
           row_number() over(partition by mon order by new_yweek) as mweek,  -- 每个月中的周顺序
           sum(decode(wday, '1', mday, null)) as Sunday,
           sum(decode(wday, '2', mday, null)) as Monday,
           sum(decode(wday, '3', mday, null)) as tuesday,
           sum(decode(wday, '4', mday, null)) as Wednesday,
           sum(decode(wday, '5', mday, null)) as Thursday,
           sum(decode(wday, '6', mday, null)) as Friday,
           sum(decode(wday, '7', mday, null)) as Saturday
      from (select dayofyear as everyday,
                   to_char(dayofyear, 'mm') as mon,
                   to_char(dayofyear, 'Month') as mon_name,
                   to_char(dayofyear, 'w') as mweek,
                   to_char(dayofyear, 'ww') as yweek,
                   case
                     when (to_char(to_date(&year || '0101', 'yyyymmdd'), 'd') > '1') and
                          (to_char(dayofyear, 'd') < to_char(to_date(&year || '0101', 'yyyymmdd'), 'd')) then
                      to_char(to_char(dayofyear, 'ww') + 1, 'fm00')
                     else
                      to_char(dayofyear, 'ww')
                   end as new_yweek,
                   to_char(dayofyear, 'd') as wday,
                   to_char(dayofyear, 'dd') as mday
              from (select to_date(&year || '0101', 'yyyymmdd') + level - 1 as dayofyear
                      from dual
                    connect by level <= to_char(to_date(&year || '1231', 'yyyymmdd'), 'ddd')))
     group by mon, mon_name, new_yweek;
  • 相关阅读:
    数据结构-循环队列程序演示
    C++进阶:新人易入的那些坑 --1.常量、常指针和指针常量
    this.$confirm的用法
    属性或方法“degreeList”不是在实例上定义的,而是在渲染期间引用的。通过初始化该属性,确保该属性是反应性的,无论是在data选项中,还是在基于类的组件中
    CSS清除浮动
    react里的高阶组件
    map和forEach的区别
    hash和history两种模式的区别
    js原型链的理解
    for..in,for..of 和forEach的区别
  • 原文地址:https://www.cnblogs.com/cczz_11/p/2516533.html
Copyright © 2011-2022 走看看