SQL VERTICA 运行 总计 30/60/90 天
SQL VERTICA running sums 30/60/90 days
嗨,我有两个 table 具有多对多关系
作为结果,我希望每条记录的得分为 table(因此 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
我认为这是一个 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;
嗨,我有两个 table 具有多对多关系
作为结果,我希望每条记录的得分为 table(因此 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
我认为这是一个 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;