OVER
Function
The OVER clause is used with window functions to define the partitioning, ordering, and range of a window. A window function calculates a value for each row based on its window, unlike regular aggregate functions which merge multiple rows into one.
Syntax
window_func ( [ expression [, ...] ] ) OVER
( [ PARTITION BY expression [, ...] ]
[ ORDER BY expression [ ASC | DESC ] [ NULLS { FIRST | LAST } ] [, ...] ]
[ window_frame ] ) Where window_frame is:
{ ROWS | RANGE } BETWEEN frame_start AND frame_end frame_start and frame_end are:
{ CURRENT ROW
| UNBOUNDED PRECEDING
| UNBOUNDED FOLLOWING
| offset PRECEDING
| offset FOLLOWING } Parameters
| Parameter | Description |
|---|---|
| window_func | The window function, including aggregate window functions (SUM, AVG, COUNT, MAX, MIN, etc.) and ranking window functions (RANK, DENSE_RANK, ROW_NUMBER, NTILE, LEAD, LAG, FIRST_VALUE, LAST_VALUE, NTH_VALUE, etc.). |
| PARTITION BY | Partitions data by one or more expressions. The window function is calculated independently within each partition. Similar to GROUP BY, but does not merge rows. |
| ORDER BY | Determines the order for window function calculation. ASC or DESC can be used to specify ascending or descending order. |
| ROWS | A physical window. Defines the window range based on row number offset. |
| RANGE | A logical window. Defines the window range based on the value offset of the ORDER BY expression. |
| CURRENT ROW | The current row. |
| UNBOUNDED PRECEDING | The window start, from the first row of the partition. |
| UNBOUNDED FOLLOWING | The window end, to the last row of the partition. |
| offset PRECEDING | Moves offset rows (ROWS) or values (RANGE) forward from the current row. |
| offset FOLLOWING | Moves offset rows (ROWS) or values (RANGE) backward from the current row. |
| NULLS FIRST | Places NULL values first. |
| NULLS LAST | Places NULL values last. |
Window Range Examples
| Window Range | Description |
|---|---|
ROWS BETWEEN CURRENT ROW AND CURRENT ROW | The window contains only the current row. |
ROWS BETWEEN 3 PRECEDING AND 5 FOLLOWING | The window spans from 3 rows before to 5 rows after the current row. |
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW | The window spans from the beginning of the partition to the current row (default window). |
ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING | The window spans from the current row to the end of the partition. |
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING | The window covers the entire partition. |
Differences Between ROWS and RANGE
- ROWS is a physical window, calculated based on row number offset, independent of the value of the current row.
- RANGE is a logical window, calculated based on the value offset of the ORDER BY expression, including all rows whose values fall within the range.
Precautions
- The OVER clause includes three parts: PARTITION BY, ORDER BY, and the window range. They can be used in combination.
- When ORDER BY is not specified, the window defaults to covering the entire partition.
- When the window range is not specified, the default window is ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW.
- When the OVER clause is empty, the window represents the entire table.
Examples
Cumulative count:
SELECT id, COUNT(id) OVER (ORDER BY id ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) FROM over_test;
Partitioned ranking:
SELECT name, class_id, score, RANK() OVER (PARTITION BY class_id ORDER BY score DESC) AS class_rank FROM student;
Moving average:
SELECT date, amount, AVG(amount) OVER (ORDER BY date ROWS BETWEEN 2 PRECEDING AND CURRENT ROW) AS moving_avg FROM sales;
Using a window alias (Spark 4.0):
SELECT name, score, RANK() OVER w AS rnk, SUM(score) OVER w AS total FROM student WINDOW w AS (PARTITION BY class_id ORDER BY score DESC);
What is your overall rating for this page?
Thank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot