如何调用以前的日期记录?

时间:2019-10-29 05:54:14

标签: sql oracle

我有一张桌子如下

===========================
+---------+----------+--------
ACC_NUM    DATE      BALANCE
+---------+----------+--------
A101         01-01-19     120 
+---------+----------+--------
A101         02-01-19     100 
+---------+----------+--------
A101         03-01-19      90 
+---------+----------+--------

我想要输出如下:

==========================
+---------+----------+--------,--------------,
ACC_NUM   . DATE     . BALANCE' PEV_DATE_BAL '
+---------+----------+--------'--------------'
A101       01-01-19        120               '
---------------------------------------------'
A101       02-01-19        100           120 '
---------------------------------------------'
A101       03-01-19         90           100 '
----------------------------------------------

什么查询可以输出如下?

1 个答案:

答案 0 :(得分:4)

使用LAG功能:

SELECT ACC_NUM
       , DATE_c
       , BALANCE
       , LAG(BALANCE) OVER (
                      ORDER BY DATE_c
                      ) PEV_DATE_BAL
FROM testTable;

这里是DEMO