目錄
- 首先我們先來看一下業務sql
select t.dept,t.day,count(*)
from(
select regexp_substr(dept), '[^,]+', 1, level) dept,day
from (
select wm_concat(dept) dept,day from baseinfo group by day
) m
connect by level<= regexp_count(dept,',') + 1
) t
group by t.dept,t.day
- baseinfo 表中的數據格式如下,僅用來舉例說明
dept day
10001,10002 2021-08-12
10003 2021-08-10
2021-08-10
10001,10002 2021-08-14
2021-08-14
10024,10046,10024,10043,10011,10015, 2021-08-14
- 對於oracle sql中的函數分析
wm_concat(dept),day ... group by day ,意思就是根據day來分組,對dept分組內所有值連接成一個集合
regexp_substr(字符串,正則,從左開始偏移-默認1-表示字符串的起點,獲取第幾個根據正則分割出來的組,默認'c'區分大小寫匹配)
regexp_count(字符串1,字符串2) 返回要匹配的字符串2 在字符串1中出現的次數,沒有則返回 0
regexp_substr(dept), '[^,]+', 1, level) from table connect by level<= 5 ,要輸出 level個子串,這里小於等於5,沒有5個就為null。顯然多余的null不是我們想要的結果
select regexp_substr(dept), '[^,]+', 1, level),day from table connect by level<= regexp_count(dept,',') + 1 ,這里會根據自謙的子查詢中的時間分組,依次返回 dept 集合中的每個子串
- 根據以上分析,得出hive sql 如下(sql 僅供參考)
select a.dept,a.day,count(*)
from(
select dept,day
from(
select split(concat_ws(',',collect_list(m.dept)),',') dept,day
from baseinfo m
group by day
) t
lateral view explode(dept) c as dept
)a
group by a.dept,a.day
最后關於hive的 lateral view explode 介紹可以看這篇博主的博文
https://blog.csdn.net/guodong2k/article/details/79459282
