SQL Datediff - find datediff between rows

后端 未结 2 2107
醉梦人生
醉梦人生 2020-12-05 05:40

I would like to query a database using sql to show the difference in time between id 1,2,3 and so on. basically it will compare the row below it for all records. any help wo

2条回答
  •  猫巷女王i
    2020-12-05 06:12

    Standard ANSI SQL solution. Should work in PostgreSQL, Oracle, DB2 and Teradata:

    SELECT idcode, 
           date_time, 
           date_time - lag(date_time) over (order by date_time) as difference
    FROM your_table
    

提交回复
热议问题