SQL VERTICA运行总计30/60/90天

时间:2018-10-11 06:40:01

标签: sql database vertica

嗨,我有两个具有多对多关系的表

enter image description here

作为结果,我希望获得评分表中的每条记录(因此id_case和id_date),我希望查看从scoring.id_date到scoring.id_date + 30天,+ 60天等的交易总数。

这是我到目前为止提出的,但是根本没有用:

  with t as  (select id_case,id_date,amount 
                    from TableTransactions 
                     ) 


            SELECT id_case,f.id_date,
                   sum(case when exists (select * from t t where t.id_case=f.id_case and t.id_date between f.id_date and (to_char(date(cast( f.id_date as varchar(8))) + 360, 'YYYYMMDD'))::int ) then t.amount else 0 end )  as Days360

            from TableScoring f

1 个答案:

答案 0 :(得分:0)

我认为这是具有条件聚合的join

select s.id_case, s.id_date,
       sum(case when t.id_date >= s.id_date and t.id_date < s.id_date + interval '30 day'
                then t.amount else 0
           end) as amount_30,
       sum(case when t.id_date >= s.id_date and t.id_date < s.id_date + interval '60 day'
                then t.amount else 0
           end) as amount_60,
       sum(case when t.id_date >= s.id_date and t.id_date < s.id_date + interval '90 day'
                then t.amount else 0
           end) as amount_90
from scoring s join
     transactions t
     on t.id_case = s.id_case
group by s.id_case, s.id_date;