Postgresql中根据条件获取上一条记录

Get the previous record based on conditions in Postgresql

我有两个 table,第一个包含交易详情,第二个包含用户的订单:

id | transaction_date
1  |   2019-01-01 
2  |   2019-02-01
3  |   2019-01-01


id | transaction_id | amount | user_id
15         1            7       1           
20         2            15      1
25         3            25      1

我想要这个结果,也就是说所有用户的订单也有他之前根据交易日期支付的金额。

user_id | amount | previous amount
   1         7           NULL
   1         15           7
   1         25           15

我尝试了多种方法,包括使用 LAG 函数,但似乎无法使用它,因为我必须加入另一个 table 才能获得 transaction_date。我想我应该用左连接做一个子查询,但我不知道如何只得到以前的订单

谢谢

这是 joinlag():

select t2.user_id, t2.amount,
       lag(t2.amount) over (partition by t2.user_id order by t1.date) as prev_amount
from table1 t1 join
     table2 t2
     on t2.transaction_id = t1.id;