2017-08-15 11 views
0

キャンペーンID、アカウント、開始日、終了日、取引日と収益のフィールドを持つ表がImpalaにあります。同じアカウントと収益の値を持つ複数のキャンペーンがあります。 [Transaction_Date、Transaction_Date + 36 months]の範囲内に収まるキャンペーン間で収益額を分割したいと考えています。
サンプルテーブル:ここ日付範囲内の行数で列の値を除算

Campaign | Account | Start Date | End Date | Trans. Date | Revenue 
     1  | 1234 | 13-05-17 | 13-06-17 | 19-10-17 | 200 
     2  | 1234 | 14-01-16 | 14-02-16 | 19-10-17 | 200 
     2  | 5678 | 14-01-16 | 14-02-16 | 07-02-16 | 200 
     3  | 2345 | 20-05-15 | 20-07-15 | 22-05-15 | 300 
     4  | 1234 | 15-10-13 | 15-11-13 | 19-10-17 | 200 
     4  | 5678 | 15-10-13 | 15-11-13 | 22-05-15 | 300 

取引日は、キャンペーン開​​始後36ヶ月を落下するので、アカウント1234年の収益は、キャンペーン1と2、及びません4との間で分割する必要があります。

Campaign | Account | Start Date | End Date | Trans. Date | Revenue | Avg Revenue 
     1  | 1234 | 13-05-17 | 13-06-17 | 19-10-17 | 200  | 100 
     2  | 1234 | 14-01-16 | 14-02-16 | 19-10-17 | 200  | 100 
     2  | 5678 | 14-01-16 | 14-02-16 | 07-02-16 | 200  | 200 
     3  | 2345 | 20-05-15 | 20-07-15 | 22-05-15 | 300  | 150 
     4  | 1234 | 15-10-13 | 15-11-13 | 19-10-17 | 200  | NULL 
     4  | 2345 | 15-10-13 | 15-11-13 | 22-05-15 | 300  | 150 

EDIT:
1の場合:
は基本的に、私は次の操作を実行するアカウント2345年の収益は、キャンペーン2と4
の間で分割されるべきであるので、結果テーブルはようでなければなりませんtrans_dateが開始日と開始日+ 3年の間にあるそのアカウントのすべての行を取得します。
2.これらの各行の収益を行数で除算します。
私はパーティションを使ってこの作業をしようとしましたが、日付の値に基づいて可変範囲を持つものを作成する方法がわかりません。
これがこれをより明確にすることを望みます。
ありがとうございます!どのRDBMS

+0

?何を試しましたか?私たちのお手伝いをしてください。 – CGritton

+0

申し訳ありません。私はImpalaを使用しています.. 私はパーティションクエリを使用しようとしましたが、さまざまな日付範囲のためにそれについて行く方法がわかりませんでした.. –

答えて

0

これは、Oracleで動作する、概念はPostgresのに適合させることができるはずです。..

drop table test; 

create table test as 
select 1 as Campaign, 1234 as Account, to_date('13-05-17', 'DD-MM-YY') as Start_Date, to_date('13-06-17', 'DD-MM-YY') as End_Date, to_date('19-10-17', 'DD-MM-YY') as Trans_Date, 200 as Revenue from dual union all 
select 2 as Campaign, 1234 as Account, to_date('14-01-16', 'DD-MM-YY') as Start_Date, to_date('14-02-16', 'DD-MM-YY') as End_Date, to_date('19-10-17', 'DD-MM-YY') as Trans_Date, 200 as Revenue from dual union all 
select 2 as Campaign, 5678 as Account, to_date('14-01-16', 'DD-MM-YY') as Start_Date, to_date('14-02-16', 'DD-MM-YY') as End_Date, to_date('07-02-16', 'DD-MM-YY') as Trans_Date, 200 as Revenue from dual union all 
select 3 as Campaign, 2345 as Account, to_date('20-05-15', 'DD-MM-YY') as Start_Date, to_date('20-07-15', 'DD-MM-YY') as End_Date, to_date('22-05-15', 'DD-MM-YY') as Trans_Date, 300 as Revenue from dual union all 
select 4 as Campaign, 1234 as Account, to_date('15-10-13', 'DD-MM-YY') as Start_Date, to_date('15-11-13', 'DD-MM-YY') as End_Date, to_date('19-10-17', 'DD-MM-YY') as Trans_Date, 200 as Revenue from dual union all 
select 4 as Campaign, 2345 as Account, to_date('15-10-13', 'DD-MM-YY') as Start_Date, to_date('15-11-13', 'DD-MM-YY') as End_Date, to_date('22-05-15', 'DD-MM-YY') as Trans_Date, 300 as Revenue from dual 
; 

select 
    a.* 
    ,case when Start_Date + (365 * 3) > Trans_Date then Revenue else null end/count(case when Start_Date + (365 * 3) > Trans_Date then 1 else null end) over (partition by account) as Avg_Revenue 
from test a 
order by Campaign, Account 
+0

こんにちは、ありがとう。これは、テストaからavg_revenueとして 'a *、revenue/count(キャンペーン)を(アカウント単位で)選択する 'というように、where句を として使用する方法と違いますか? –

+0

where句は、サンプルデータの5番目の行を結果から除外します。私の方法にはそれが含まれていますが、平均収入はゼロです。 –

+0

ああ大丈夫..ありがとう。結果を確認してお知らせします。 編集:私は前のコメントで完全なクエリを追加していないことを知った。レコードの場合は、 'start_date +(365 * 3)> trans_date'のテストaからavg_revenueとして、a。*、revenue/count(キャンペーン単位) –

関連する問題