sql >> Database teknologi >  >> RDS >> Sqlserver

Beregn tidsforskellen mellem to rækker

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

I 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. Liste over SQL Server Comparison Operators

  2. Sådan konverteres en streng til en numerisk værdi i PostgreSQL

  3. Hvordan ser man logfiler i MySQL?

  4. Tilsløring af følsomme data i dine eksekveringsplaner