-1

如果插入任何新行,我需要填充一个将存储上一个交易日期的列。

我正在寻找这样的东西

Customer ID | first txn date | txn date  | last txn date | prev txn dt
   1        |   01-Jun-20    | 01-Jun-20 | 27-Sep-21     | null
   1        |   01-Jun-20    | 24-Dec-20 | 27-Sep-21     | 01-Jun-20
   1        |   01-Jun-20    | 20-Sep-21 | 27-Sep-21     | 24-Dec-20
   1        |   01-Jun-20    | 27-Sep-21 | 27-Sep-21     | 20-Sep-21
4

1 回答 1

0

在 mysql 8+ 中:

select * 
  , MIN(txndate) over (partition by CustomerID) firsttxndate
  , MAX(txndate) over (partition by CustomerID) lasttxndate
  , LAG(txndate) over (partition by customerID order by txndate) as prvtxndate
from table
于 2021-10-05T17:46:54.177 回答