• 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;
  • 相关阅读:
    详述Xgboost原理
    Uncaught Error: Script error for "popper.js", needed by: bootstrap
    JS 正则表达式从地址中提取省市县
    OneThink实现多图片批量上传功能
    移动端图片上传解决方案localResizeIMG先压缩后ajax无刷新上传
    PHPCMS V9添加模板自定义全局变量
    CentOS 7:如何安装防火墙?
    HTML5的fieldset标签
    PHP使用CURL详解
    asp.net+swfupload 多图片批量上传(附源码下载)
  • 原文地址:https://www.cnblogs.com/cczz_11/p/2516533.html
Copyright © 2020-2023  润新知