Introduction
ROWS
11. Abbreviations
RANGE
Default window frame
Revision

Instruction

Fine! If our window frame has CURRENT ROW as one of the boundaries, we can also use some abbreviated syntax to make things easier:

  • ROWS UNBOUNDED PRECEDING means BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
  • ROWS n PRECEDING means BETWEEN n PRECEDING AND CURRENT ROW
  • ROWS CURRENT ROW means BETWEEN CURRENT ROW AND CURRENT ROW

The same rules applies for FOLLOWING. As a way of example, the following query:

SELECT
  id,
  name,
  introduced,
  COUNT(id) OVER(ORDER BY introduced ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
FROM product;

Can be rewritten to:

SELECT
  id,
  name,
  introduced,
  COUNT(id) OVER(ORDER BY introduced ROWS UNBOUNDED PRECEDING)
FROM product;

Exercise

You will now have a chance to practice abbreviations. Pick those stock changes which refer to product_id = 3. For each of them, show the id, changed date, quantity, and the running total, indicating the current stock status. Sort the rows by the changed date in the ascending order.

Stuck? Here's a hint!

Use the abbreviated form: ROWS UNBOUNDED PRECEDING.

Console

Code editor

Result

TableConsole