私はあるページの訪問者の数を格納するテーブル(view_of_referred_events
)を持っています。現在の日付が失われている場合、30日の移動/移動合計
date country_id referral product_id visitors
2016-04-01 216 pl 113759 1
2016-04-03 216 pl 113759 1
2016-04-06 216 pl 113759 13
2016-04-07 216 pl 113759 10
この商品の30日間の移動/移動合計は、欠落している場合でも計算したいと考えています。私は別のcountry_id
、referral
とproduct_id
の数十を持っているので、
date country_id referral product_id cumulative_visitors
2016-04-01 216 pl 113759 1
2016-04-02 216 pl 113759 1
2016-04-03 216 pl 113759 2
2016-04-04 216 pl 113759 2
2016-04-05 216 pl 113759 2
2016-04-06 216 pl 113759 15
2016-04-07 216 pl 113759 25
さて、これは単純化した表現は、次のとおりです。だから、最終的な結果は以下のようなものでなければなりません。 {date
、country_id
、referral
、product_id
}の可能なすべての組み合わせを持つテーブルを事前に作成することはできません。これは、テーブルのサイズを考えると無意味になるためです。特定の{date
,country_id
,referral
およびproduct_id
}が以前に存在しなかった場合、私は最終表に1列あることも望んでいません。
view_of_referred_events
にその日の訪問者がいない場合、前の行(前日)の値を使用するようにImpalaに指示する簡単な方法があれば、私は考えていました。
私はこのクエリを書いています。list_of_dates
は、4月1日から4月7日までの日数の一覧表です。
select
t.`date`,
t.country_id,
t.referral,
t.product_id,
sum(visitors) over (partition by t.country_id, t.referral, t.product_id order by t.`date`
rows between 30 preceding and current row) as cumulative_sum_visitors
from (
selec
d.`date`,
re.country_id,
re.referral,
re.product_id,
sum(visitors) as visitors
from list_of_dates d
left outer join view_of_referred_events re on d.`date` = re.`date`
and re.referral = "pl"
and re.product_id = "113759"
and re.country_id = "216"
group by d.`date`, re.country_id, re.referral, re.product_id
) t
order by t.`date` asc;
これは、私が欲しいものに似ていますが、正確ではありません。
date country_id referral product_id cumulative_visitors
2016-04-01 216 pl 113759 1
2016-04-02 NULL NULL NULL NULL
2016-04-03 216 pl 113759 2
2016-04-04 NULL NULL NULL NULL
2016-04-05 NULL NULL NULL NULL
2016-04-06 216 pl 113759 15
2016-04-07 216 pl 113759 25
明確にしてください:あなたのテキストとタイトルが累積和に関するものです。クエリでは、30日の移動/移動合計が必要です。 –