LAG

From Oracle FAQ
Jump to: navigation, search

LAG is a SQL analytical function that can makes data from prior rows available without having to do a self join.

Example[edit]

SQL> SELECT c1 "Curr",
  2         LAG(c1) OVER (ORDER BY id) "Prev",
  3         c1 - LAG(c1) OVER (ORDER BY id) "Diff"
  4    FROM t1;
      Curr       Prev       Diff
---------- ---------- ----------
        40
        45         40          5
        35         45        -10
        37         35          2

Also see[edit]

  • LEAD, look at values of leading rows.