Last active
October 20, 2023 08:22
-
-
Save sh16ma/70597081fd0bf9c5f3e9bcedb0d1eeec to your computer and use it in GitHub Desktop.
#💎 #TreasuerData #集計テク #Presto #SQL #構成比
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/********************************************************* | |
* ID-POS | |
* | |
* POSデータのDWH(約80億レコード/1テーブル)を全て回すのはリスク。 | |
* 「WHERE句」を用いて、レコード取得制限の掛ける絞り込み。 | |
*********************************************************/ | |
with | |
PREP_POS as ( | |
select | |
td_time_parse( | |
case | |
when crm_channel_kubun = '2' then substr(ec_juchu_nichiji, 1, 10) | |
else eigyo_bi | |
end, 'JST') as time -- UNIXTIME | |
, case | |
when crm_channel_kubun = '2' then substr(ec_juchu_nichiji, 1, 10) | |
else eigyo_bi | |
end as eigyo_bi | |
, case | |
when crm_channel_kubun = '2' then ec_dempyo_key | |
else dempyo_key | |
end as dempyo_key | |
, kaiin_master_code | |
, sotozei_kingaku | |
, crm_channel_kubun | |
from | |
crm_db.crm_getsuji_kaiin_meisai | |
where | |
td_time_range(time, '2021-08-15', '2021-10-15', 'JST') --※ココで大幅にデータを絞り込む | |
and crm_jigyo_kubun = '2' --**** | |
and crm_torihiki_kubun = '2' --**** | |
and sotozei_kingaku > 0 --**** | |
) | |
, REPORT_POS as ( | |
select | |
eigyo_bi | |
, count(distinct case when crm_channel_kubun='1' then kaiin_master_code end) as st_user | |
, count(distinct case when crm_channel_kubun='2' then kaiin_master_code end) as ec_user | |
, count(distinct case when crm_channel_kubun='1' then dempyo_key end) as st_purchase | |
, count(distinct case when crm_channel_kubun='2' then dempyo_key end) as ec_purhase | |
, sum(case when crm_channel_kubun='1' then sotozei_kingaku end) as st_amount | |
, sum(case when crm_channel_kubun='2' then sotozei_kingaku end) as ec_amount | |
from | |
PREP_POS | |
where | |
td_time_range(time, '2021-09-01', '2021-10-01', 'JST') | |
group by | |
eigyo_bi | |
order by | |
eigyo_bi | |
) | |
select * from REPORT_POS | |
; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Cf.