在統(tǒng)計一些指標時,通常會有多個指標需要分組進行聚合,但是數(shù)據(jù)源的粒度可能并非是指標分組的粒度。舉個例子,比如從訪客表中提取訪客的數(shù)據(jù),每行數(shù)據(jù)有每個平臺的首次訪問時間;另外要做一個平臺統(tǒng)計表,其中的一個指標統(tǒng)計的是各個平臺近1天、7天、30天的新訪客。這里的數(shù)據(jù)源的粒度是一行一個訪客,帶個平臺時間字段,但指標粒度是一行一個平臺,兩者不相同。
先加上標簽,一行多列變一行一列
????????由于數(shù)據(jù)源需要統(tǒng)計的字段是多個,因此也沒有辦法根據(jù)字段進行g(shù)roup by 聚合,只能普通的先count(if(時間篩選條件)),一次性將所有的1天、7天以及30天的各個平臺新增訪客統(tǒng)計出來于一行,并加上標簽區(qū)分平臺(時間不用區(qū)分,因為30天人數(shù)>7天>1天),可以外層套層sql用concat(人數(shù),'-',平臺)再collect_set()聚合到一個數(shù)組中,,我這里用的是odpsSQL的map()函數(shù).這時候數(shù)據(jù)變成了一行一列
再將數(shù)據(jù)從一行一列變成一行多列
????????這時我們只需要通過lateral view explode() 進行展開成一列多行。
拆解標簽變成多行多列帶分組標簽字段
????????再拆解出標簽,通過split()將平臺劃分成單獨的一個字段后,
將統(tǒng)計指標字段group by存入同一個標簽分組中
????????根據(jù)平臺進行g(shù)roup by ,再將1天、7天、30天的數(shù)據(jù)collect_set()裝入同一個數(shù)組中。
對數(shù)組中的數(shù)據(jù)進行處理得出字段
????????將數(shù)組通過排序,通過數(shù)組下標直接獲取對應(yīng)時間的新增訪客數(shù)。文章來源:http://www.zghlxwxcb.cn/news/detail-621315.html
總的代碼如下:文章來源地址http://www.zghlxwxcb.cn/news/detail-621315.html
select
platform,
first_visit_count_list[0] as first_visit_count_1d,
first_visit_count_list[1] as first_visit_count_7d ,
first_visit_count_list[2] as first_visit_count_30d
from (
select
platform,
array_sort(collect_SET(first_visit_count),(l, r) -> CASE WHEN l < r THEN -1L WHEN l > r THEN 1L ELSE 0L END) as first_visit_count_list -- 30天>7天>1天,根據(jù)該規(guī)律排序數(shù)組即可
from (
select
map( -- 存放在一個map里,便于展開
-- 30天
count(if(ssish_first_time>to_date(${bizdate-30},'yyyymmdd'),1,null)),'主站',
count(if(mkf_first_time>to_date(${bizdate-30},'yyyymmdd'),1,null)),'my客蜂',
count(if(cps_first_time>to_date(${bizdate-30},'yyyymmdd'),1,null)),'cps',
count(if(wechatlink_first_time>to_date(${bizdate-30},'yyyymmdd'),1,null)),'微鏈',
count(if(nyboss_first_time>to_date(${bizdate-30},'yyyymmdd'),1,null)),'保思',
count(if(wxmall_first_time>to_date(${bizdate-30},'yyyymmdd'),1,null)),'微信商城',
-- 7天
count(if(ssish_first_time>to_date(${bizdate-7},'yyyymmdd'),1,null)),'主站',
count(if(mkf_first_time>to_date(${bizdate-7},'yyyymmdd'),1,null)),'my客蜂',
count(if(cps_first_time>to_date(${bizdate-7},'yyyymmdd'),1,null)),'cps',
count(if(wechatlink_first_time>to_date(${bizdate-7},'yyyymmdd'),1,null)),'微鏈',
count(if(nyboss_first_time>to_date(${bizdate-7},'yyyymmdd'),1,null)),'保思',
count(if(wxmall_first_time>to_date(${bizdate-7},'yyyymmdd'),1,null)),'微信商城',
-- 昨天
count(if(ssish_first_time>to_date(${bizdate},'yyyymmdd'),1,null)),'主站',
count(if(mkf_first_time>to_date(${bizdate},'yyyymmdd'),1,null)),'my客蜂',
count(if(cps_first_time>to_date(${bizdate},'yyyymmdd'),1,null)),'cps',
count(if(wechatlink_first_time>to_date(${bizdate},'yyyymmdd'),1,null)),'微鏈',
count(if(nyboss_first_time>to_date(${bizdate},'yyyymmdd'),1,null)),'保思',
count(if(wxmall_first_time>to_date(${bizdate},'yyyymmdd'),1,null)),'微信商城'
)
as platform_first_visit_count_list
from nanyan_space.dim_visitors_info
where pt='${bizdate}'
)a1
lateral view explode(platform_first_visit_count_list) tmp as first_visit_count,platform
group by platform
)a2
到了這里,關(guān)于SQL實現(xiàn)一行數(shù)據(jù)分組后轉(zhuǎn)多行多列的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!