Sqlserver
 sql >> Teknologi Basis Data >  >> RDS >> Sqlserver

Hitung Selisih Waktu Antara Dua Baris

WITH    rows AS
        (
        SELECT  *, ROW_NUMBER() OVER (ORDER BY DataDate) AS rn
        FROM    mytable
        )
SELECT  DATEDIFF(second, mc.DataDate, mp.DataDate)
FROM    rows mc
JOIN    rows mp
ON      mc.rn = mp.rn - 1

Di SQL Server 2012+:

SELECT  DATEDIFF(second, pDataDate, dataDate)
FROM    (
        SELECT  *,
                LAG(dataDate) OVER (ORDER BY dataDate) pDataDate
        FROM    rows
        ) q
WHERE   pDataDate IS NOT NULL


  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Ubah Notifikasi dengan Sql Server 2008

  2. Periksa Jenis Parameter Fungsi Partisi di SQL Server (T-SQL)

  3. Bagaimana RANK() Bekerja di SQL Server

  4. 5 Manfaat Pemantauan Kinerja Basis Data Proaktif

  5. Nilai kolom identitas tiba-tiba melompat ke 1001 di server sql