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

SQL Server DATEDIFF beregnet kolonne mellem rækker

Jeg anbefaler dig at bruge en visning til dette formål:

CREATE VIEW Table1_vw 
AS
WITH cte AS (
    SELECT  *, 
            ROW_NUMBER() OVER (ORDER BY RecordTime) AS rn
    FROM    dbo.Table1
)
SELECT  mc.RecordTime,
        mc.Running,
        mc.Fault,
        DATEDIFF(second, mc.RecordTime, mp.RecordTime) Diff
FROM cte mc
LEFT JOIN cte mp
ON mc.rn = mp.rn - 1

Da du bruger SQL Server 2012 du kan bruge LEAD funktion:

CREATE VIEW Table1_vw 
AS
SELECT  RecordTime,
        Running,
        Fault,
        DATEDIFF(second,RecordTime,LEAD(RecordTime,1,NULL) OVER (ORDER BY RecordTime ASC) ) as Diff
FROM Table1
GO


  1. Skjuler ægte databaseobjekt-id i url'er

  2. OPENXML i Postgres

  3. Sådan konfigurerer du MySQL til at være case-sensitive

  4. Opret en binær streng af nuller med variabel længde