我当前的派生数据集如下所示
--------------------------------------------------------------------
| Type_ID | component | timestamp | status |
-------------------------------------------------------------------
| 52 | G1 |Aug 25, 2014 9:10:47.0 AM | IN |
| 53 | G1 |Aug 27, 2014 11:11:29.0 AM| OUT |
| 58 | G1 |Aug 27, 2014 11:11:29.0 AM| OUT |
| 53 | G1 |Aug 27, 2014 11:11:29.0 AM| OUT |
| | G1 |Aug 27, 2014 11:11:29.0 AM| GEN |
| 52 | G2 |Aug 28, 2014 11:11:29.0 AM| IN |
| 52 | G1 |Aug 29, 2014 11:11:29.0 AM| IN |
| | G2 |Aug 29, 2014 12:11:29.0 AM| GEN |
--------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------
| Type_ID | component | timestamp | status | Type_Id | component | timestamp | status |
-------------------------------------------------------------------------------------------------------------------------------
| | G1 |Aug 27, 2014 11:11:29.0 AM | GEN | 52 | G1 |Aug 25, 2014 9:10:47.0 AM | IN |
| | G2 |Aug 29, 2014 12:11:29.0 AM | GEN | 52 | G2 |Aug 28, 2014 11:11:29.0 AM| IN |
所以根据状态IN,我想获得TYPE_ID''的先前记录,尝试使用LAG,但无法根据STATUS = IN动态获取以前的记录,
数据库:SAP HANA, SQL会很有帮助
答案 0 :(得分:0)
SELECT *, LAG(TYPE_ID) OVER (ORDER BY TIMESTAMP), LAG(STATUS) OVER (ORDER BY TIMESTAMP), LAG(TIMESTAMP) OVER (ORDER BY TIMESTAMP) FROM TAB
WHERE STATUS IN ('IN')
ORDER BY TIMESTAMP;
首先,我们将消除所有不需要的记录,并在新结果集上使用滞后。