我使用滞后函数来获取上一个日期但没有获得所需的结果
Client Account# Trade Date Amount Prev Txn Date Output Received Sum
A 1 1/1/2016 10 - 0
A 1 1/1/2016 20 - 1/1/2016 0
A 1 1/1/2016 30 - 1/1/2016 0
A 1 1/4/2017 40 1/1/2016 1/1/2016 60
A 1 1/4/2017 50 1/1/2016 1/4/2017 60
如何获取上一个txn日期列而不是输出接收列。我正在使用滞后功能 此外,我需要计算交易日期之前发生的交易总和。 我使用了以下代码
select
a.*,
lag(trade_date) over (partition by client, account# order by trade_date) next_txn_date_2
from tmp_burst_activity_4
答案 0 :(得分:0)
首先获取每天的总金额,客户和帐户,然后使用lag
,最后将此结果加入原始表格。
select t.account,t.client,t.trade_dt,t.amount,s.prev_trade_date,s.prev_total
from tbl t
join (select account,client,trade_dt
,lag(trade_dt) over(partition by account,client order by trade_dt) as prev_trade_date
,lag(total) over(partition by account,client order by trade_dt) as prev_total
from (select account,client,trade_dt,sum(amount) as total
from tbl
group by account,client,trade_dt
) t
) s on s.account=t.account and s.client=t.client and s.trade_dt=t.trade_dt