2017-02-17 4 views
0

私は以下のような巨大な段落ハイブクエリのコードがあります。重複した期間に複数のカウント(別名...)でハイブクエリを最適化するにはどうすればよいですか?

select 
count(distinct case when click_day between ${hiveconf:dt_180} and ${hiveconf:dt_end} and recommend_flag=1 then productid else null end) as unique_hk_products_cnt_180d, 
count(distinct case when click_day between ${hiveconf:dt_90} and ${hiveconf:dt_end} and recommend_flag=1 then productid else null end) as unique_hk_products_cnt_90d, 
count(distinct case when click_day between ${hiveconf:dt_30} and ${hiveconf:dt_end} and recommend_flag=1 then productid else null end) as unique_hk_products_cnt_30d, 
count(distinct case when click_day between ${hiveconf:dt_15} and ${hiveconf:dt_end} and recommend_flag=1 then productid else null end) as unique_hk_products_cnt_15d, 
count(distinct case when click_day between ${hiveconf:dt_7} and ${hiveconf:dt_end} and recommend_flag=1 then productid else null end) as unique_hk_products_cnt_7d 
from mytable ; 

をこれらのフィールドの唯一の違いは、時間ウィンドウの長さを表し日の数、です。 これは私のクエリを非常に大きくし、間違いを作るのは難しいです。

dt_15は、前に定義されただけで文字列変数です:

set dt_15 = CONCAT(SUBSTRING(date_sub(current_date,15), 1, 4), SUBSTRING(date_sub(current_date,15), 6, 2), SUBSTRING(date_sub(current_date,15), 9, 2)); 

どれでもみんなは私がよりシンプルにそれを再構築することができますか?新しいテーブルのproductフィールドへのループを使用したいのですか?

ありがとうございました。

答えて

0

この

select count (case when click_day between ${hiveconf:dt_180} and ${hiveconf:dt_end} then productid end) as unique_hk_products_cnt_180d 
     ,count (case when click_day between ${hiveconf:dt_90} and ${hiveconf:dt_end} then productid end) as unique_hk_products_cnt_90d 
     ,count (case when click_day between ${hiveconf:dt_30} and ${hiveconf:dt_end} then productid end) as unique_hk_products_cnt_30d 
     ,count (case when click_day between ${hiveconf:dt_15} and ${hiveconf:dt_end} then productid end) as unique_hk_products_cnt_15d 
     ,count (case when click_day between ${hiveconf:dt_7} and ${hiveconf:dt_end} then productid end) as unique_hk_products_cnt_7d 

from (select click_day,recommend_flag,productid 
       ,row_number() over 
       (
        partition by productid 
        order by  click_day desc  
       ) as rn 

     from mytable 

     where click_day between ${hiveconf:dt_180} and ${hiveconf:dt_end} 
      and recommend_flag=1 
     ) t 

where rn = 1 

P.S.をお試しください
日付を非標準形式で保存する理由は何ですか?

+0

出力フィールドは変更できません。すなわち、結果は水平のテーブルでなければなりません。 – yanachen

+0

それはまだ同じです –

0

これを試してみてください。このよう値を設定するための 使用ビルドの日付機能

set dt_15 = from_unixtime(unix_timestamp(date_sub(current_date,15),'yyyy-mm-dd'),'yyyymmdd') 

は、連結とサブストリング操作を削除します。

select 
count(case when click_day between ${hiveconf:dt_180} and ${hiveconf:dt_end} then productid else null end) as unique_hk_products_cnt_180d, 
count(case when click_day between ${hiveconf:dt_90} and ${hiveconf:dt_end} then productid else null end) as unique_hk_products_cnt_90d, 
count(case when click_day between ${hiveconf:dt_30} and ${hiveconf:dt_end} then productid else null end) as unique_hk_products_cnt_30d, 
count(case when click_day between ${hiveconf:dt_15} and ${hiveconf:dt_end} then productid else null end) as unique_hk_products_cnt_15d, 
count(case when click_day between ${hiveconf:dt_7} and ${hiveconf:dt_end} then productid else null end) as unique_hk_products_cnt_7d 
from (select distinct click_day,productid where recommend_flag = 1) tmp ; 

これにより、入力音量が小さくなります。 click_day < dt_endをすべての列で同じにしておき、その間を削除することもできます。

+0

元の投稿と同じロジックを持っていません。 click_dayごとにproductidの重複を削除しても、ある期間の重複したproductidをカウントすることはできません。 –

関連する問題