Oracle
 sql >> Base de Dados >  >> RDS >> Oracle

Atualizar a contagem de dias anteriores usando a função LAG no ORACLE


Você pode usar MERGE:
MERGE INTO PRD_DTLS A USING 
(
  SELECT
      PRODUCT,
      TXN_DATE,
      LAG(NO_OF_TXNS) OVER(
          PARTITION BY PRODUCT
          ORDER BY
              TXN_DATE
      ) AS PREV_TXN
  FROM
      PRD_DTLS 
)
B ON ( A.PRODUCT = B.PRODUCT
       AND A.TXN_DATE = B.TXN_DATE )
WHEN MATCHED THEN UPDATE SET A.PREV_DAY_TXN = CASE
    WHEN B.PREV_TXN IS NOT NULL THEN B.PREV_TXN
    ELSE 0
END;

Felicidades!!