Oracle日历表维护实践:建表、准备数据

欢迎来到我的博客!

在公司里,日历表及相关参数必定是极其重要的,它的准确性影响着整个业务,随着业务的复杂,获取各种奇葩日期可能往往会耗费很大精力。

一、基础日历表、日期参数表创建

表结构图示:

基础日历表:
Oracle日历表维护实践:建表、准备数据
日期参数表:
Oracle日历表维护实践:建表、准备数据

建表语句:

  • 创建基础日历表
--创建基础日历表
-- Create table
create table SCOTT.D_DATE
(
  date_code           NUMBER(8) not null,
  date_desc           VARCHAR2(30 CHAR),
  wk_of_year_code     NUMBER(8),
  wk_of_year_lbl      VARCHAR2(10 CHAR),
  wk_of_year_desc     VARCHAR2(30 CHAR),
  mth_code            NUMBER(8),
  mth_lbl             VARCHAR2(10 CHAR),
  mth_desc            VARCHAR2(30 CHAR),
  quar_code           NUMBER(8),
  quar_lbl            VARCHAR2(10 CHAR),
  quar_desc           VARCHAR2(30 CHAR),
  six_months_code     NUMBER(8),
  six_months_desc     VARCHAR2(30 CHAR),
  year_code           NUMBER(8),
  year_desc           VARCHAR2(30 CHAR),
  wk_start_flag       VARCHAR2(30 CHAR),
  wk_end_flag         VARCHAR2(30 CHAR),
  mth_start_flag      VARCHAR2(30 CHAR),
  mth_end_flag        VARCHAR2(30 CHAR),
  quar_start_flag     VARCHAR2(30 CHAR),
  quar_end_flag       VARCHAR2(30 CHAR),
  year_start_flag     VARCHAR2(30 CHAR),
  year_end_flag       VARCHAR2(30 CHAR),
  trd_daily_flag      CHAR(1 CHAR),
  etl_date            TIMESTAMP(6),
  hkex_trd_daily_flag CHAR(1 CHAR),
  hgt_trd_daily_flag  CHAR(1 CHAR)
);
-- Add comments to the table 
comment on table SCOTT.D_DATE
  is '日期维表';
-- Add comments to the columns 
comment on column SCOTT.D_DATE.date_code
  is '日期代码';
comment on column SCOTT.D_DATE.date_desc
  is '日期描述';
comment on column SCOTT.D_DATE.wk_of_year_code
  is '年中周代码';
comment on column SCOTT.D_DATE.wk_of_year_desc
  is '年中周描述';
comment on column SCOTT.D_DATE.mth_code
  is '月份代码';
comment on column SCOTT.D_DATE.mth_desc
  is '月份描述';
comment on column SCOTT.D_DATE.quar_code
  is '季代码';
comment on column SCOTT.D_DATE.quar_desc
  is '季描述';
comment on column SCOTT.D_DATE.six_months_code
  is '半年代码';
comment on column SCOTT.D_DATE.six_months_desc
  is '半年描述';
comment on column SCOTT.D_DATE.year_code
  is '年代码';
comment on column SCOTT.D_DATE.year_desc
  is '年描述';
comment on column SCOTT.D_DATE.wk_start_flag
  is '周初标志';
comment on column SCOTT.D_DATE.wk_end_flag
  is '周末标志';
comment on column SCOTT.D_DATE.mth_start_flag
  is '月初标志';
comment on column SCOTT.D_DATE.mth_end_flag
  is '月末标志';
comment on column SCOTT.D_DATE.quar_start_flag
  is '季初标志';
comment on column SCOTT.D_DATE.quar_end_flag
  is '季末标志';
comment on column SCOTT.D_DATE.year_start_flag
  is '年初标志';
comment on column SCOTT.D_DATE.year_end_flag
  is '年末标志';
comment on column SCOTT.D_DATE.trd_daily_flag
  is '交易日标志';
comment on column SCOTT.D_DATE.etl_date
  is 'ETL日期';
comment on column SCOTT.D_DATE.hkex_trd_daily_flag
  is '港交所交易日标志';
comment on column SCOTT.D_DATE.hgt_trd_daily_flag
  is '沪股通交易日标志';
-- Create/Recreate primary, unique and foreign key constraints 
alter table SCOTT.D_DATE
  add constraint PK_D_DATE primary key (DATE_CODE)
  using index ;
  • 日期参数表
--创建参数表
-- Create table
create table SCOTT.DATE_VARIABLE_PARAMETER
(
  txdate         NUMBER(8) not null,
  variable_code  VARCHAR2(50) not null,
  variable_name  VARCHAR2(50) not null,
  variable_descr VARCHAR2(100),
  variable_value VARCHAR2(100)
);
-- Add comments to the table 
comment on table SCOTT.DATE_VARIABLE_PARAMETER
  is '日期参数表';
-- Add comments to the columns 
comment on column SCOTT.DATE_VARIABLE_PARAMETER.txdate
  is '执行日期';
comment on column SCOTT.DATE_VARIABLE_PARAMETER.variable_code
  is '变量代码';
comment on column SCOTT.DATE_VARIABLE_PARAMETER.variable_name
  is '变量名称';
comment on column SCOTT.DATE_VARIABLE_PARAMETER.variable_descr
  is '变量描述';
comment on column SCOTT.DATE_VARIABLE_PARAMETER.variable_value
  is '变量值';
-- Create/Recreate primary, unique and foreign key constraints
alter table SCOTT.DATE_VARIABLE_PARAMETER
  add constraint PK_DATE_VARIABLE_PARAMETERPK_ primary key (TXDATE, VARIABLE_CODE);

二、日期数据插入基础日历表

详细步骤如下:

Oracle日历表维护实践:建表、准备数据

sql语句如下:

--sql语句
SELECT DATE_CODE
       ,TRD_DAILY_FLAG
       ,HKEX_TRD_DAILY_FLAG
       ,HGT_TRD_DAILY_FLAG
  FROM SCOTT.D_DATE
  FOR UPDATE;

继续操作

Oracle日历表维护实践:建表、准备数据

下载基础日历表数据excel
下载地址(坚果云):
点击下载
打开excel进行继续如下操作

Oracle日历表维护实践:建表、准备数据

Oracle日历表维护实践:建表、准备数据

!!!选择commit提交数据后,查询结果表是否有数据,若提示是否将改变的记录写入数据库,选择是。

谢谢阅读!

本作品采用《CC 协议》,转载必须注明作者和本文链接
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!