oracle:如何用sql生成日历

简介: BI分析中,经常需要将事实表与时间维度表关联起来,按年/月/日来逐层展示,常用的做法是创建一张日历表,结构类似如下: create table T_BAS_CALENDAR ( d_year NUMBER(4) not null, d_month NUMBER(2) not n...

BI分析中,经常需要将事实表与时间维度表关联起来,按年/月/日来逐层展示,常用的做法是创建一张日历表,结构类似如下:

create table T_BAS_CALENDAR
(
  d_year  NUMBER(4) not null,
  d_month NUMBER(2) not null,
  d_day   NUMBER(2) not null
);
comment on table T_BAS_CALENDAR
  is '日历表';
comment on column T_BAS_CALENDAR.d_year
  is '';
comment on column T_BAS_CALENDAR.d_month
  is '';
comment on column T_BAS_CALENDAR.d_day
  is '';
alter table T_BAS_CALENDAR
  add constraint PK_BAS_CALENDAR primary key (D_YEAR, D_MONTH, D_DAY);

但是如何向这张表批量插入日历数据,方法就很多了,下面是仅用SQL语言生成日历的参考方法:

 1 create or replace procedure P_IMPORT_CALENDAR(p_year_start number,
 2                                               p_year_end   number) is
 3   cmonth    integer;
 4   cyear     integer;
 5   cday      integer;
 6   day_first integer;
 7   day_last  integer;
 8 begin
 9   --生成从p_year_start到p_year_end的所有日历 created by yjmyzz@126.com 2015-04-27
10   
11   --firstly,delete history records
12   delete from T_BAS_CALENDAR where d_year between p_year_start and p_year_end;
13   for cyear in p_year_start .. p_year_end loop
14     for cmonth in 1 .. 12 loop
15       --get first-day of Month
16       select to_number(cyear || lpad(cmonth, 2, '0') || '01', '99999999')
17         into day_first
18         from dual;
19       --last-day of Month
20       select to_number(to_char(add_months(to_date(day_first, 'yyyyMMdd'), 1) - 1,
21                                'yyyyMMdd'),
22                        '99999999')
23         into day_last
24         from dual;
25       for cday in day_first .. day_last loop   
26         --insert to table  
27         INSERT INTO T_BAS_CALENDAR
28           (D_YEAR, D_MONTH, D_DAY)
29         VALUES
30           (CYEAR, CMONTH, SUBSTR(cday, 7));
31       end loop;
32     end loop;
33   end loop;
34   commit;
35 end P_IMPORT_CALENDAR;

 

目录
相关文章
|
2月前
|
SQL Oracle 关系型数据库
|
2月前
|
SQL Oracle 关系型数据库
MySQL、SQL Server和Oracle数据库安装部署教程
数据库的安装部署教程因不同的数据库管理系统(DBMS)而异,以下将以MySQL、SQL Server和Oracle为例,分别概述其安装部署的基本步骤。请注意,由于软件版本和操作系统的不同,具体步骤可能会有所变化。
125 3
|
2月前
|
SQL 存储 Oracle
TDengine 3.3.2.0 发布:新增 UDT 及 Oracle、SQL Server 数据接入
**TDengine 3.3.2.0 发布摘要** - 开源与企业版均强化性能,提升WebSocket、stmt模式写入与查询效率,解决死锁,增强列显示。 - taos-explorer支持geometry和varbinary类型。 - 企业版引入UDT,允许自定义数据转换。 - 新增Oracle和SQL Server数据接入。 - 数据同步优化,支持压缩,提升元数据同步速度,错误信息细化,支持表名修改。 - 扩展跨平台支持,包括麒麟、Euler、Anolis OS等。
81 0
|
3月前
|
SQL Oracle 关系型数据库
一些非常有用的Oracle SQL
一些非常有用的Oracle SQL
38 4
|
3月前
|
SQL Oracle 关系型数据库
mysql和oracle 命令行执行sql文件 数据库执行sql文件 执行sql语句
mysql和oracle 命令行执行sql文件 数据库执行sql文件 执行sql语句
59 0
|
SQL Oracle 关系型数据库
Oracle SQL优化之多表连接
Oracle SQL优化之多表连接
538 0
Oracle SQL优化之多表连接
|
SQL 关系型数据库 Oracle
ORACLE SQL优化之ORA-03150&ORA-02055&ORA-02063
                                                                                                             >   
4832 0
|
SQL 存储 Oracle
Oracle SQL语句优化方法总结
  1、SQL语句尽量用大写的   因为oracle总是先解析SQL语句,把小写的字母转换成大写的再执行。   2、使用表的别名   当在SQL语句中连接多个表时, 尽量使用表的别名并把别名前缀于每个列上。这样一来,   就可以减少解析的时间并减少那些由列歧义引起的语法错误。   3、选择最有效率的表名顺序(只在基于规则的优化器(RBO)中有效)
199 0
|
SQL Oracle 关系型数据库

推荐镜像

更多