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 | Go to the first row. | Go to the first row. |
UNBOUNDED FOLLOWING | Go to the last row. | Go to the last row. |
<N> PRECEDING | First N rows. | Go to the row whose value is greater than or equal to the value obtained by subtracting <N> from the OrderBy column value. |
<N> FOLLOWING | Last N rows. | Go to the row whose value is less than or equal to the value obtained by adding <N> to the OrderBy column value. |
Function Name | Feature Description | Function Parameter | Supported Type |
ROW_NUMBER() | Marks row numbers for data in each partition. | - | - |
RANK() | Performs non-intensive data sorting in each partition. | - | - |
DENSE_RANK() | Performs intensive data sorting in each partition. | - | - |
LEAD(<expr>, <offset>, <default>) | Calculates the values of <offset> rows after the current row. If no rows meet the condition, <default> is returned. | [Required] <expr>: Calculated column. | All types (three parameters except the Time type). |
| | [Optional] <offset>: Number of rows to offset after the current row. The default value is 1. | Value type |
| | [Optional] <default>: Default return value when the calculated row does not meet the condition. The default value is NULL. | Consistent with the <expr> type. |
LAG(<expr>, <offset>, <default>) | Calculates the values of <offset> rows before the current row. If no rows meet the condition, <default> is returned. | [Required] <expr>: Calculated column. | All types (three parameters except the Time type). |
| | [Optional] <offset>: Number of rows to offset before the current row. The default value is 1. | Value type |
| | [Optional] <default>: Default return value when the calculated row does not meet the condition. The default value is NULL. | Consistent with the <expr> type. |
FIRST_VALUE(<expr>) | Calculates the first value in a window in a partition. | [Required] <expr>: Calculated column. | All types. |
LAST_VALUE(<expr>) | Calculates the last value in a window in a partition. | [Required] <expr>: Calculated column. | All types. |
MIN(<expr>) | Calculates the <expr> value of the row corresponding to the minimum value in the OrderBy column in a window in a partition. | [Required] <expr>: Calculated column. | All types. |
MAX(<expr>) | Calculates the <expr> value of the row corresponding to the maximum value in the OrderBy column in a window in a partition. | [Required] <expr>: Calculated column. | All types. |
COUNT(<expr>) | Counts the total number of rows of data in a window in a partition. | [Required] <expr>: Calculated column. | All types. |
SUM(<expr>) | Calculates the total data volume in a window in a partition. | [Required] <expr>: Calculated column. | Value type |
AVG(<expr>) | Calculates the average value of data in a window in a partition. | [Required] <expr>: Calculated column. | Value 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 statementmysql> 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 one row behind)| 1 | 6 | 1 | -- Window row index range: current (no data behind)+----+----+----+6 rows in set (0.06 sec)
-- Sample statementmysql> 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 | -- Window row index range: current -> row index corresponding to 3 [0–2]| 1 | 1 | 3 || 1 | 2 | 2 | -- Window row index range: current -> row index corresponding to 4 [2–3]| 1 | 4 | 3 | -- Window row index range: current -> row index corresponding to 6 [3–5]| 1 | 6 | 2 | -- Window row index range: current -> row index corresponding to 8 [4–5]| 1 | 6 | 2 |+----+----+----+6 rows in set (0.06 sec)
-- Sample statementmysql> 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 statementselectc2, 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 statementmysql> 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 statementmysql> 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 statementmysql> 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 statementmysql> 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 statementmysql> 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 statementmysql> 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 statementmysql> 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 statementmysql> 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)
Apakah halaman ini membantu?
Anda juga dapat Menghubungi Penjualan atau Mengirimkan Tiket untuk meminta bantuan.
masukan