oracle日期維度表

2021-10-24 14:13:35 字數 4258 閱讀 8206

查詢日期屬性

select

to_number(to_char(sysdate,'yyyymmdd')) as id,

trunc(sysdate) as fdate,--日期

to_char(sysdate,'yyyy') || '-q' || to_char(sysdate,'q') as year_quarter,--年季度

to_char(sysdate,'yyyy') || '-' || to_char(sysdate,'mm') as year_month,--年月

to_char(sysdate,'yyyy') || '-' || to_char(sysdate,'ddd') as year_day,--年天

to_char(sysdate,'yyyy-mm-dd') as year_month_day,--年月日

extract(year from sysdate) as year,--年份

to_number(to_char(sysdate,'q')) as quarter_of_year,--當年季度

extract(month from sysdate) as month_of_year,--當年月份

months_between(trunc(sysdate, 'mm'),trunc(sysdate, 'q'))+1 as month_of_quarter,--當季度月份

to_number(to_char(sysdate, 'ww')) as week_of_year,--當年周

ceil((trunc(sysdate)-trunc(sysdate, 'q')+1)/7) as week_of_quarter,--當季度周

to_number(to_char(sysdate, 'w')) as week_of_month,--當月周

to_number(to_char(sysdate,'ddd')) as day_of_year,--當年天

trunc(sysdate)-trunc(sysdate, 'q')+1 as day_of_quarter,--當季度天

extract(day from sysdate) as day_of_month,--當月天

trunc(sysdate)-trunc(sysdate,'iw')+1 as day_of_week,--當周天

to_char(sysdate, 'day') as week,--星期

case

when trunc(sysdate,'q')=trunc(sysdate, 'yyyy') then 1

else 0

end as is_f_quarter_of_year,--是否當年第一季度

case

when add_months(trunc (sysdate, 'year'), 12) - 1 = trunc(add_months(sysdate,3), 'q') -1 then 1

else 0

end as is_l_quarter_of_year,--是否當年最後一季度

case

when trunc(sysdate, 'mm')=trunc(sysdate, 'yyyy') then 1

else 0

end as is_f_month_of_year,--是否當年第一月

case

when trunc (last_day (sysdate)) = add_months(trunc (sysdate, 'year'), 12) - 1 then 1

else 0

end as is_l_month_of_year,--是否當年最後一月

case

when trunc(sysdate,'q') = trunc(sysdate, 'mm') then 1

else 0

end as is_f_month_of_quarter,--是否當季第一月

case

when trunc(add_months(sysdate,3), 'q') -1 = trunc (last_day (sysdate)) then 1

else 0

end as is_l_month_of_quarter,--是否當季最後一月

case

when trunc(sysdate)-trunc(sysdate, 'yyyy') < 7 then 1

else 0

end as is_f_week_of_year,--是否當年第一周

case

when to_number(to_char(sysdate,'ddd'))-52*7 > 0 then 1

else 0

end as is_l_week_of_year,--是否當年最後一周

case

when trunc(sysdate) - trunc(sysdate,'q') < 7 then 1

else 0

end as is_f_week_of_quarter,--是否當季第一周

case

when ceil((trunc(sysdate) - trunc(sysdate, 'q') +1)/7) = ceil(((trunc(add_months(sysdate,3), 'q') -1) - trunc(sysdate, 'q') + 1)/7) then 1

else 0

end as is_l_week_of_quarter,--是否當季最後一周

case

when trunc(sysdate) - trunc(sysdate, 'mm') < 7 then 1

else 0

end as is_f_week_of_month,--是否當月第一周

case

when ceil((trunc(sysdate) - trunc(sysdate, 'mm') + 1)/7) = ceil((trunc(last_day (sysdate)) - trunc(sysdate, 'mm') + 1)/7) then 1

else 0

end as is_l_week_of_month,--是否當月最後一周

case

when trunc(sysdate) = trunc(sysdate, 'yyyy') then 1

else 0

end as is_f_day_of_year,--是否當年第一天

case

when trunc(sysdate) = (add_months (trunc (sysdate, 'year'), 12) - 1) then 1

else 0

end as is_l_day_of_year,--是否當年最後一天

case

when trunc(sysdate) = trunc(sysdate, 'q') then 1

else 0

end as is_f_day_of_quarter,--是否當季第一天

case

when trunc(sysdate) = (trunc(add_months(sysdate,3), 'q') -1) then 1

else 0

end as is_l_day_of_quarter,--是否當季最後一天

case

when trunc(sysdate) = trunc(sysdate, 'mm') then 1

else 0

end as is_f_day_of_month,--是否當月第一天

case

when trunc(sysdate) = trunc (last_day (sysdate)) then 1

else 0

end as is_l_day_of_month,--是否當月最後一天

case

when trunc(sysdate) = trunc(sysdate,'iw') then 1

else 0

end as is_f_day_of_week,--是否當周第一天

case

when trunc(sysdate) = (trunc(sysdate,'iw')+6) then 1

else 0

end as is_l_day_of_week,--是否當周最後一天

case

when mod(extract(year from sysdate),4) = 0 then 1

else 0

end as is_leap--是否閏年

from dual;

日期維度表生成 Python

業務中經常需要去分析時間序列相關的資料,可能會用到年,月,日,小時,星期,節假日等等,尤其是在零售和電商行業中,可能需要分析日銷售的高峰時段等等,所以能夠有這樣一張多維度細顆粒的時間表其實用起來是非常簡便的,話不多說,直接貼 import pandas as pd from datetime imp...

5 2 3 SSAS 維度 日期維度

維度 日期維度的建立 1 日期維度 日期維度存在於幾乎所有的cube中,它是最最普遍和最重要的維度之一。要建立日期維度,首先需要建立乙個日期維度表,並編寫填充資料的儲存過程。日期維度表dimdate如下 drop table dimdate create table dimdate datekey ...

乙個sql生成hive日期維度表

目錄 1 日期維度表 2 生成語句 3 用例 num欄位名 字段中文名 描述資料型別 1date 日期日期 yyymmdd格式 bigint 2week 星期,數字型 星期,數字型 0 6 bigint 3week cn 星期中文名 星期中文名 星期一 string 4year weeks 一年中的...