日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

一个sql生成hive日期维度表

發布時間:2024/7/23 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 一个sql生成hive日期维度表 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

目錄

1、日期維度表

2、生成語句

3、用例


在進行日期處理時,有時候會很麻煩,于是小編開發了一張日期維表,供大家參考。

1、日期維度表

num字段名字段中文名描述數據類型
1date日期日期 yyyMMdd格式bigint
2week星期,數字型星期,數字型 0-6bigint
3week_cn星期中文名星期中文名 星期一……string
4year_weeks一年中的第幾周一年中的第幾周 1 2 3……bigint
5mon_dt本周周一日期本周周一日期bigint
6sun_dt本周周日日期本周周日日期bigint
7month年月年月,yyyyMM格式bigint
8month_short月份簡寫月份簡寫,MM格式1~12bigint
9month_cn月份中文名月份中文名 一月……string
10quarter季度季度,yyyyQ1\2\3\4string
11quarter_short季度 數字型季度 數字型 1-4bigint

?

2、生成語句

set hive.execution.engine=tez;
with dates as (
select date_add("2010-01-01", a.pos) as d
from (select posexplode(split(repeat("o", datediff("2030-12-31", "2010-01-01")), "o"))) a
)
insert overwrite table dim.dim_date
select
? ? d
? , date_format(d, 'yyyyMMdd000000') as to_pt ? ? ? ? ? ?-- 指定分區格式
? , date_format(d, 'yyyyMMdd') ? ? ? as date_yyyymmdd
? , trunc(d,'MM') ? ? ? ? ? ? ? ? ? ?as month_first_day?
? , last_day(d) ? ? ? ? ? ? ? ? ? ? ?as month_last_day
? , date_format(last_day(d),'yyyyMMdd000000') ? as month_last_pt
? , date_format(d, 'yyyyMM') ?as month_yyyymm
? , date_format(d, 'yyyy-MM') as month_yyyy_mm
? , month(d) as month
? , date_format(d, 'u') as week
? , date_format(d, 'E') as week_long? ?
? , weekofyear(d) as week_of_year
? , year(d) as year
? , floor(substr(d,6,2)/3.1)*3+1 as quarter
? -- , concat_group('"',date_format(d, 'yyyyMM'),'"') as date_yyyymmdd_list ? -- 低版本hive group_concat 不可用
from dates

3、用例

  • 取月末:where date_pk = month_last_day;
  • 取周末:where week_int in (6,7);
  • 取每月最后一天pt+ 當月昨天pt:where pt IN ( SELECT max(to_pt) FROM dim.dim_date where to_pt <= '${-1d_pt}' group by month_yyyymm ?);
  • 其它用法......
  • 總結

    以上是生活随笔為你收集整理的一个sql生成hive日期维度表的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。