数据库时间维度表

建表语句

CREATE TABLE risk.d_calendar(
calendar_id number NOT NULL,
date_name varchar2(200),
date_name_cn varchar2(200),
calendar_date date,
current_day number(10,0),
month_id number,
month_name varchar2(200),
month_name_cn varchar2(200),
month_number number(10,0),
month_name_short varchar2(200),
month_name_short_cn varchar2(200),
days_in_month number(10,0),
first_of_month number(10,0),
last_month_id number(10,0),
month_end number(10,0),
quarter_id number,
quarter_name varchar2(200),
quarter_name_cn varchar2(200),
quarter_name_short varchar2(200),
quarter_name_short_cn varchar2(200),
year_id number,
year_name varchar2(200),
year_name_cn varchar2(200),
year_week_name varchar2(500),
create_date timestamp,
month_week_num varchar2(100),
month_week_begin varchar2(100),
month_week_end varchar2(100),
half_year varchar2(100),
weekend_flag varchar2(100),
holidays_flag varchar2(100),
workday_flag varchar2(100)
);

数据样例

select
      to_number(to_char(sysdate,'yyyyMMdd')) calendar_id,
      to_char(sysdate,'yyyy-MM-dd') date_name,
      to_char(sysdate,'yyyy')||'年'||to_char(sysdate,'MM')||'月'||to_char(sysdate,'dd')||'日' date_name_cn,
      trunc(sysdate) calendar_date, 
      to_number(to_char(sysdate,'dd')) current_day,
      to_number(to_char(sysdate,'yyyyMM')) month_id, 
      to_char(sysdate,'yyyy-MM') month_name,
      to_char(sysdate,'yyyy')||'年'||to_char(sysdate,'MM')||'月' month_name_cn,
      to_number(to_char(sysdate,'MM')) month_number,  
      to_char(sysdate,'MM') month_name_short,
      to_char(sysdate,'MM')||'月' month_name_short_cn,
      to_number(to_char(last_day(sysdate),'dd')) days_in_month, 
      to_number(to_char(trunc(sysdate,'MM'),'yyyyMMdd')) first_of_month, 
      to_number(to_char(add_months(sysdate,-1),'yyyyMM')) last_month_id,
      to_number(to_char(last_day(sysdate),'yyyyMMdd')) month_end,
      to_number(to_char(sysdate,'yyyyq')) quarter_id, 
      to_char(sysdate,'yyyy-q') quarter_name,
      to_char(sysdate,'yyyy')||'年'||to_char(sysdate,'q')||'季度' quarter_name_cn,
      to_char(sysdate,'q') quarter_name_short,
      to_char(sysdate,'q')||'季度' quarter_name_short_cn,
      to_number(to_char(sysdate,'yyyy')) year_id,
      to_char(sysdate,'yyyy') year_name,
      to_char(sysdate,'yyyy')||'年' year_name_cn,
      to_char(sysdate,'yyyy')||'年'||to_char(sysdate,'iw')||'周' year_week_name,
      systimestamp create_date, 
      to_number(to_char(sysdate-1,'d')) month_week_num,
      to_char(trunc(sysdate,'d')+1,'yyyy-MM-dd') month_week_begin,
      to_char(trunc(sysdate,'d')+7,'yyyy-MM-dd') month_week_end,
      case when to_number(to_char(sysdate,'MM'))<7 then to_char(sysdate,'yyyy')||'年上半年' else to_char(sysdate,'yyyy')||'年下半年' end half_year,
      null weekend_flag,
      null holidays_flag,
      case when to_number(to_char(sysdate,'d')) in (1,7) then '非工作日' else '工作日' end workday_flag
  from dual;

你可能感兴趣的:(Oralce,数据库,时间维度)