Sqlserver
 sql >> Datenbank >  >> RDS >> Sqlserver

Wie erhält man den vorherigen und aktuellen Zeilenwert mit rekursivem CTE?

Dies geht von steigenden ID-Werten aus und behandelt Lücken in der ID

SELECT
   ID, 
   This.Number AS CurrentValue, 
   Prev2.Number AS PreviousValue
FROM
   myTable This
   OUTER APPLY
   (
    SELECT TOP 1
       Number
    FROM
       myTable Prev
    WHERE
       Prev.ID < This.ID  -- change to number if you want
    ORDER BY
       Prev.ID DESC
   ) Prev2;

ODER

WITH CTE
     AS (SELECT ID,
                Number,
                ROW_NUMBER() OVER (ORDER BY ID) AS rn
         FROM   Mytable)
SELECT ID,
       This.Number AS CurrentValue,
       Prev.Number AS PreviousValue
FROM   CTE This
       LEFT JOIN CTE Prev
         ON Prev.rn + 1 = This.rn; 

Und für SQL Server 2012

SELECT
   ID,
   Number AS CurrentValue,
   LAG(Number) OVER (ORDER BY ID) AS PreviousValue
FROM
   MyTable