私は以下のような巨大な段落ハイブクエリのコードがあります。重複した期間に複数のカウント(別名...)でハイブクエリを最適化するにはどうすればよいですか?
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フィールドへのループを使用したいのですか?
ありがとうございました。
出力フィールドは変更できません。すなわち、結果は水平のテーブルでなければなりません。 – yanachen
それはまだ同じです –