SELECT<WindowFunction> OVER (PARTITION BY <expr_list> ORDER BY <expr_list> [ASC / DESC] [<WindowFrame>])FROMtb_test_window;
select studentid,departmentid,classid,math,row_number() over(partition by departmentid,classid order by math) as row_numfrom student_scores;
Frame | ROWS Scenario | RANGE Scenario |
CURRENT ROW | Current row. | All rows identical to the current row. |
UNBOUNDED PRECEDING | To the first row. | To the first row. |
UNBOUNDED FOLLOWING | To the last row. | To the last row. |
<N> PRECEDING | Previous N rows. | To the rows with values greater than or equal to the OrderBy column value minus <N>. |
<N> FOLLOWING | Subsequent N rows. | To the rows with values less than or equal to the OrderBy column value plus <N>. |
Function Name | Description | Function Parameter | Supported Type |
ROW_NUMBER() | Marks the row number for data within each partition. | - | - |
RANK() | Performs non-dense ranking for data within each partition. | - | - |
DENSE_RANK() | Performs dense ranking for data within each partition. | - | - |
LEAD(<expr>,<offset>,<default>) | Calculates the value of the row that follows the current row by <offset> rows, and returns <default> if no row meets the condition. | [Required] <expr>: Calculation column. | All types (except Time types for three parameters). |
| | [Optional] <offset>: Number of rows to offset from the current row, 1 by default. | Numeric data type. |
| | [Optional] <default>: Default return value if no calculation row meets the condition, NULL by default. | Consistent with the <expr> type. |
LAG(<expr>,<offset>,<default>) | Calculates the value of the row that precedes the current row by <offset> rows, and returns <default> if no row meets the condition. | [Required] <expr>: Calculation column. | All types (except Time types for three parameters). |
| | [Optional] <offset>: Number of rows to offset from the current row, 1 by default. | Numeric data type. |
| | [Optional] <default>: Default return value if no calculation row meets the condition, NULL by default. | Consistent with the <expr> type. |
FIRST_VALUE(<expr>) | Calculates the first value in the partition window. | [Required] <expr>: Calculation column. | All types. |
LAST_VALUE(<expr>) | Calculates the last value in the partition window. | [Required] <expr>: Calculation column. | All types. |
MIN(<expr>) | Calculates the <expr> value of the row corresponding to the minimum value in the OrderBy column of the partition window. | [Required] <expr>: Calculation column. | All types. |
MAX(<expr>) | Calculates the <expr> value of the row corresponding to the maximum value in the OrderBy column of the partition window. | [Required] <expr>: Calculation column. | All types. |
COUNT(<expr>) | Calculates the total number of rows in the partition window. | [Required] <expr>: Calculation column. | All types. |
SUM(<expr>) | Calculates the total sum of data in the partition window. | [Required] <expr>: Calculation column. | Numeric data type. |
AVG(<expr>) | Calculates the average value of data in the partition window. | [Required] <expr>: Calculation column. | Numeric data type. |
drop table if exists test.tb_window;create table test.tb_window (c1 int not null primary key, c2 int, c3 int);create table test.tb_window (c1 Int32, c2 Nullable(Int32), c3 Nullable(Int32)) engine = LibraTree order by (c1);insert into test.tb_window values (1, 1, 1), (2, 1, 1), (3, 1, 2), (4, 1, 4), (5, 1, 6), (6, 1, 6);
-- Sample statementsmysql> select c2, c3, COUNT(c1) over (partition by c2 order by c3 ROWS BETWEEN CURRENT ROW AND 2 FOLLOWING) cn from test.tb_window;+----+----+----+| c2 | c3 | cn |+----+----+----+| 1 | 1 | 3 | -- Window row index range: current -> next 2 rows [0-2]| 1 | 1 | 3 | -- Window row index range: current -> next 2 rows [1-3]| 1 | 2 | 3 | -- Window row index range: current -> next 2 rows [2-4]| 1 | 4 | 3 | -- Window row index range: current -> next 2 rows [3-5]| 1 | 6 | 2 | -- Window row index range: current -> next 1 row [4-5] (only 1 row after it)| 1 | 6 | 1 | -- Window row index range: current (no data after it)+----+----+----+6 rows in set (0.06 sec)
-- Sample statementsmysql> select c2, c3, COUNT(c1) over (partition by c2 order by c3 RANGE BETWEEN CURRENT ROW AND 2 FOLLOWING) cn from test.tb_window;+----+----+----+| c2 | c3 | cn |+----+----+----+| 1 | 1 | 3 | --| 1 | 1 | 3 || 1 | 2 | 2 | --| 1 | 4 | 3 | --| 1 | 6 | 2 | --| 1 | 6 | 2 |+----+----+----+6 rows in set (0.06 sec)
-- Sample statementsmysql> select c2, c3, ROW_NUMBER() over (partition by c2 order by c3) rn from test.tb_window;+----+----+------+| c2 | c3 | rn |+----+----+------+| 1 | 1 | 1 || 1 | 1 | 2 || 1 | 2 | 3 || 1 | 4 | 4 || 1 | 6 | 5 || 1 | 6 | 6 |+----+----+------+6 rows in set (0.04 sec)
-- Sample statementsselectc2, c3,RANK() over (partition by c2 order by c3) rk,DENSE_RANK() over (partition by c2 order by c3) drkfrom test.tb_window;+------+------+------+------+| c2 | c3 | rk | drk |+------+------+------+------+| 1 | 1 | 1 | 1 || 1 | 1 | 1 | 1 || 1 | 2 | 3 | 2 || 1 | 4 | 4 | 3 || 1 | 6 | 5 | 4 || 1 | 6 | 5 | 4 |+------+------+------+------+6 rows in set (0.05 sec)
-- Sample statementsmysql> selectc2, c3,LEAD(c3) over (partition by c2 order by c3) ld,LAG(c3) over (partition by c2 order by c3) lgfrom test.tb_window;+------+------+------+------+| c2 | c3 | ld | lg |+------+------+------+------+| 1 | 1 | 1 | NULL || 1 | 1 | 2 | 1 || 1 | 2 | 4 | 1 || 1 | 4 | 6 | 2 || 1 | 6 | 6 | 4 || 1 | 6 | NULL | 6 |+------+------+------+------+6 rows in set (0.11 sec)
-- Sample statementsmysql> selectc2, c3,LEAD(c3, 2) over (partition by c2 order by c3) ld,LAG(c3, 2) over (partition by c2 order by c3) lgfrom test.tb_window;+------+------+------+------+| c2 | c3 | ld | lg |+------+------+------+------+| 1 | 1 | 2 | NULL || 1 | 1 | 4 | NULL || 1 | 2 | 6 | 1 || 1 | 4 | 6 | 1 || 1 | 6 | NULL | 2 || 1 | 6 | NULL | 4 |+------+------+------+------+6 rows in set (0.07 sec)
-- Sample statementsmysql> selectc2, c3,LEAD(c3, 2, 1000) over (partition by c2 order by c3) ld,LAG(c3, 2, 1000) over (partition by c2 order by c3) lgfrom test.tb_window;+------+------+------+------+| c2 | c3 | ld | lg |+------+------+------+------+| 1 | 1 | 2 | 1000 || 1 | 1 | 4 | 1000 || 1 | 2 | 6 | 1 || 1 | 4 | 6 | 1 || 1 | 6 | 1000 | 2 || 1 | 6 | 1000 | 4 |+------+------+------+------+6 rows in set (0.10 sec)
-- Sample statementsmysql> selectc2, c3,FIRST_VALUE(c3) over (partition by c2 order by c3 ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) fv,LAST_VALUE(c3) over (partition by c2 order by c3 ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) lvfrom test.tb_window;+------+------+------+------+| c2 | c3 | fv | lv |+------+------+------+------+| 1 | 1 | 1 | 6 || 1 | 1 | 1 | 6 || 1 | 2 | 1 | 6 || 1 | 4 | 1 | 6 || 1 | 6 | 1 | 6 || 1 | 6 | 1 | 6 |+------+------+------+------+6 rows in set (0.07 sec)
-- Sample statementsmysql> selectc2, c3,MIN(c3) over (partition by c2 order by c3 ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) mi,MAX(c3) over (partition by c2 order by c3 ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) mafrom test.tb_window;+------+------+------+------+| c2 | c3 | mi | ma |+------+------+------+------+| 1 | 1 | 1 | 6 || 1 | 1 | 1 | 6 || 1 | 2 | 1 | 6 || 1 | 4 | 1 | 6 || 1 | 6 | 1 | 6 || 1 | 6 | 1 | 6 |+------+------+------+------+6 rows in set (0.07 sec)
-- Sample statementsmysql> select c2, c3, COUNT(c3) over (partition by c2 order by c3 ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) c from test.tb_window;+------+------+----+| c2 | c3 | c |+------+------+----+| 1 | 1 | 6 || 1 | 1 | 6 || 1 | 2 | 6 || 1 | 4 | 6 || 1 | 6 | 6 || 1 | 6 | 6 |+------+------+----+6 rows in set (0.04 sec)
-- Sample statementsmysql> select c2, c3, SUM(c3) over (partition by c2 order by c3 ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) s from test.tb_window;+------+------+------+| c2 | c3 | s |+------+------+------+| 1 | 1 | 20 || 1 | 1 | 20 || 1 | 2 | 20 || 1 | 4 | 20 || 1 | 6 | 20 || 1 | 6 | 20 |+------+------+------+6 rows in set (0.06 sec)
-- Sample statementsmysql> select c2, c3, AVG(c3) over (partition by c2 order by c3 ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) a from test.tb_window;+------+------+--------+| c2 | c3 | a |+------+------+--------+| 1 | 1 | 3.3333 || 1 | 1 | 3.3333 || 1 | 2 | 3.3333 || 1 | 4 | 3.3333 || 1 | 6 | 3.3333 || 1 | 6 | 3.3333 |+------+------+--------+6 rows in set (0.06 sec)
Was this page helpful?