-- Create table

create table TEMP_CALENDAR

(

  MONTH VARCHAR2(6),

  W_7   VARCHAR2(2),

  W_1   VARCHAR2(2),

  W_2   VARCHAR2(2),

  W_3   VARCHAR2(2),

  W_4   VARCHAR2(2),

  W_5   VARCHAR2(2),

  W_6   VARCHAR2(2),

  WEEK  VARCHAR2(20)

) ;

-- Add comments to the table 

comment on table TEMP_CALENDAR

  is '日期源数据表';

-- Add comments to the columns 

comment on column TEMP_CALENDAR.MONTH

  is '月份(格式如:200801)';

comment on column TEMP_CALENDAR.W_7

  is '周日的日期(如:06)';

comment on column TEMP_CALENDAR.W_1

  is '周一(如:07)';

comment on column TEMP_CALENDAR.W_2

  is '周二(如:08)';

comment on column TEMP_CALENDAR.W_3

  is '周三(如:09)';

comment on column TEMP_CALENDAR.W_4

  is '周四(如:10)';

comment on column TEMP_CALENDAR.W_5

  is '周五(如:11)';

comment on column TEMP_CALENDAR.W_6

  is '周六(如:12)';

comment on column TEMP_CALENDAR.WEEK

  is '本月第几周';

----生成 日历信息 

-- truncate table calendar; 

declare 





    vi_begin_year number(6); 

    vi_end_year number(6); 

    vi_year number(6); 

    vi_month number(6); 

    vs_month varchar2(6); 

    vs_first_day varchar2(8); 









begin 





    vi_begin_year :=2000; ---start year

    vi_end_year :=2100;   --- end year

    vi_month :=1; 

    vi_year := vi_begin_year; 





loop exit when(vi_year > vi_end_year);

 for i in 1..12 loop 

     if length(i) =1 then 

        vs_month := to_char(vi_year)||'0'||to_char(i); 

     else

         vs_month := to_char(vi_year)||to_char(i); 

     end if;

     

  vs_first_day := vs_month||'01'; 

  

  insert into temp_calendar ( month, week,w_7, w_1, w_2, w_3, w_4, w_5, w_6 ) 

  

  select substr(vs_first_day,1,6) month, 

  ceil((to_char(everyday,'dd')+(to_char(to_date(vs_first_day,'yyyymmdd'),'d')-1))/7) as week, 

  sum(decode(to_char(everyday,'dy'),'星期日',to_char(everyday,'dd'))) as 星期日, 

  sum(decode(to_char(everyday,'dy'),'星期一',to_char(everyday,'dd'))) as 星期一, 

  sum(decode(to_char(everyday,'dy'),'星期二',to_char(everyday,'dd'))) as 星期二, 

  sum(decode(to_char(everyday,'dy'),'星期三',to_char(everyday,'dd'))) as 星期三, 

  sum(decode(to_char(everyday,'dy'),'星期四',to_char(everyday,'dd'))) as 星期四, 

  sum(decode(to_char(everyday,'dy'),'星期五',to_char(everyday,'dd'))) as 星期五, 

  sum(decode(to_char(everyday,'dy'),'星期六',to_char(everyday,'dd'))) as 星期六 

  from(

    select to_date(vs_first_day,'yyyymmdd') + level - 1 as everyDay 

    from dual 

    connect by level <= (last_day(to_date(vs_first_day,'yyyymmdd')) 

    - to_date(vs_first_day,'yyyymmdd') +1) ) 

  group by ceil((to_char(everyday,'dd')+(to_char(to_date(vs_first_day,'yyyymmdd'),'d')-1))/7) ; 

  

  

  commit; 

  

  end loop; 

  

  vi_year := vi_year+1; 

  

end loop; 

  

  end;

  order by a.year,a.month, a.week;

oralce sql 创建指定时间段内的日历信息的更多相关文章

  1. sql取指定时间段内的所有月份

    declare @begin datetime,@end datetime set @begin='2017-01-01' set @end='2019-03-04' declare @months ...

  2. sqlServer通过指定的起始时间,创建该时间段内以年、月、日为时间段的临时表

    通过指定的起始时间,创建该时间段内以年.月.日为时间段的临时表 ALTER PROCEDURE [dbo].[YOUR_SP_Name]     -- Add the parameters for t ...

  3. 趣味SQL——创建指定的数据类型

    原创作品,出自 "深蓝的blog" 博客,深蓝的blog:http://blog.csdn.net/huangyanlong/article/details/46908843 趣味 ...

  4. SQL Server备份时间段内插入的数据依旧进入了备份文件?(转载)

    问 MSSql我在本机测试了下.为了延长备份时间,找个大的数据库.开始完整备份bak然后再此库新建表,并增添数据.备份结束.==================还原备份后,在还原的数据库内发现新增的表 ...

  5. shell查找指定时间段内的文件

    #!/bin/bash#20170905 输入参数格式echo "显示"$1"的备份文件"date_0=$1date_1=`expr $date_0 + 1`d ...

  6. SQL Server中获取指定时间段内的所有日期

    DECLARE @days INT, @date_start DATETIME = '2016-11-01', @date_end DATETIME = '2016-11-10' SET @days ...

  7. 数据库SQL语句查询指定时间段内的数据

    [摘要]有的时候,我们需要查询数据库某段时间之间的数据,比如2016年5月1号到到5月3号之间用户注册数量(特殊节假日期间)等.那么用SQL语句如何实现呢? 首先,数据表中的存时间的字段比如是addt ...

  8. SQL Server中获取指定时间段内的所有月份

    例如查询 2012-1-5 到 2012-11-3 之间所有的月份 declare @begin datetime,@end datetime set @begin='2012-1-5' set @e ...

  9. SQL Server 查询时间段内数据

    方式一: ALTER Proc [dbo].[usp_Rpt_AcctTypeAudit] @FromDate datetime=null, -- yyyy-mm-dd (may change in ...

随机推荐

  1. BZOJ 4800 折半暴搜

    思路: 把它拆成两半  分别搜一发 两部分分别排好序 用two-pointers扫一遍 就可以了. (读入也要用long long) //By SiriusRen #include <cstdi ...

  2. 日期Date和String/Long之间的转换

    下面是关于日期的常见的几种类型转换: import java.text.ParseException; import java.text.SimpleDateFormat; import java.u ...

  3. 设置浏览器让js报错

    ie-工具---internet选项--高级--“禁用脚本提示”前面那个框的勾去掉---“显示每个脚本错误的通知”给该项打勾 注意:此时是静态页面很容易提示出错误的行号,但是当js是动态页面的时候,浏 ...

  4. MySQL笔试题搜罗

    一.有表如下 +------+---------+--------+ | name | subject | score | +------+---------+--------+ | 张三 | 数学 ...

  5. 【Oracle】DG中物理备库、快照备库的相互转换

    一.物理备库切换快照备库 1. 如果正在运行日志应用,先停止 ALTER DATABASE RECOVER MANAGED STANDBY DATABASE CANCEL; 2. 确保数据库为MOUN ...

  6. kafkaAPI

    使用方法见官网API介绍: https://kafka.apache.org/22/javadoc/index.html?org/apache/kafka/clients/consumer/Kafka ...

  7. PAT_A1015#Reversible Primes

    Source: PAT A1015 Reversible Primes (20 分) Description: A reversible prime in any number system is a ...

  8. html第三节课

    表单 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.o ...

  9. vue: This relative module was not found

    这是今天运行vue项目报的一个错误,特地在此记录一下. 错误信息如下: ERROR Failed to compile with 1 errors This relative module was n ...

  10. 第2章 this 、 call 和 apply

    第一部分 基础知识 第2章  this . call 和 apply 2.1  this JavaScript的 this 总是指向一个对象,而具体指向哪个对象是在运行时基于函数的执行环境动态绑定的, ...