unbounded
and current row
— not with interval distance as shown belowThe over
accepts range
framing, which limits the scope of the window functions to the rows between the specified range of values relative to the value of the current row.
SELECT *, COUNT(*) OVER(ORDER BY dt
RANGE BETWEEN INTERVAL '1' DAY PRECEDING
AND CURRENT ROW)
FROM …
Returns an additional column with the row count of the rows that have the order by
value up to 1 day less than the current row (interval '1' day preceding
) and all rows that hat the same order by
value as the current row (current row
).
Meaningful framing requires an order by
clause in over
as well. Note that this order by
clause does not necessarily affect the order of rows in the final result. If you want the result in a specific order, add an order by
clause at the outermost query level.
Note that some systems that support over(… range between …)
for numeric sort keys don’t support it on temporal data types.
Sub clauses:
Product specific extensions: over(range between…)
(strings, multi-columns)
The over
clause, including range between
with a timestamp
order by
key, is defined in ISO/IEC 9075:2016-2 as part of optional feature T611, “Elementary OLAP operations”.
You can’t catch up on 20 years of SQL evolution in one day. Subscribe the newsletter via E-Mail, Twitter or RSS to gradually catch up and to keep modern-sql.com on your radar.
The essence of SQL tuning in 200 pages
Buy now!
(paperback and/or PDF)
Paperback also available at Amazon.com.
Markus offers SQL training and consulting for developers working at companies of all sizes.
Learn more »