2016-08-29 48 views
-1

我要計算2之間的差值記錄我的表用了2列ID和時間戳我要計算記錄之間的時間增量Vertica的SQL三角洲

id |timestamp |delta 
---------------------------------- 
    1 |100  |0 
    2 |101  |1 (101-100) 
    3 |106  |5 (106-101) 
    4 |107  |1 (107-106) 

我與Vertica的數據的基礎工作,我想在我的數據庫上創建該表的視圖/投影。

是否有可能創建這個計算而不使用udf函數?

回答

2

您可以使用lag()用於此目的:

select id, timestamp, 
     coalesce(timestamp - lag(timestamp) over (order by id), 0) as delta 
from t;