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;
PARTITION BY and ORDER BY must be column references from a table. Arbitrary expressions are not currently supported.ORDER BY clause does not currently support NULLS FIRST or NULLS LAST. NULL values are treated as the minimum value by default in sorting operations.IGNORE NULLS, RESPECT NULLS, FROM FIRST, and FROM LAST modifiers are not currently supported.ROWS and RANGE frame types. The GROUPS frame and the EXCLUDE clause are not currently supported.Frame | ROWS Scenario | RANGE Scenario |
CURRENT ROW | Current row. | All rows whose ORDER BY column values are the same as the current row. |
UNBOUNDED PRECEDING | To the first row of the partition. | To the first row of the partition. |
UNBOUNDED FOLLOWING | To the last row of the partition. | To the last row of the partition. |
PRECEDING | Offset N rows forward from the current row. | To the row whose ORDER BY column value is greater than or equal to (current row value - N). |
FOLLOWING | Offset N rows backward from the current row. | To the row whose ORDER BY column value is less than or equal to (current row value + N). |
ROWS | RANGE <Frame>.ROWS | RANGE BETWEEN <Frame> AND <Frame>.ROW_NUMBER, RANK, DENSE_RANK) and value-fetching functions (LEAD, LAG) are not restricted by <WindowFrame>.Function Name | Capability Description | Function Parameter | Supported Type |
ROW_NUMBER() | Assigns a row number to the data within each partition according to the ORDER BY sequence. | - | - |
RANK() | Performs a non-dense ranking on the data within each partition (identical values share the same rank, and the next rank is skipped). | - | - |
DENSE_RANK() | Performs a dense ranking on the data within each partition (identical values share the same rank, and the next rank is consecutive). | - | - |
LEAD(, , ) | Returns the value of <expression> at the row offset <offset> backward from the current row, and returns <default> when out of bounds. | [Required]: The column to compute. | All types. |
| | [Optional]: The number of rows to offset backward from the current row, with a default value of 1. | Integer types (INT, BIGINT, and so on) |
| | [Optional] <default>: The default value to return when out of bounds, with a default of NULL. | The type must be consistent with the type of the specified column, or it can be accommodated by the type of that column. |
LAG(, , ) | Returns the value of <expression> at the row offset <offset> forward from the current row, and returns <default> when out of bounds. | [Required]: The column to compute. | All types. |
| | [Optional]: The number of rows to offset forward from the current row, with a default value of 1. | Integer types (INT, BIGINT, and so on) |
| | [Optional] <default>: The default value to return when out of bounds, with a default of NULL. | The type must be consistent with the type of the specified column, or it can be accommodated by the type of that column. |
FIRST_VALUE() | Returns the value of <expression> from the first row within the window frame. | [Required]: The column to compute. | All types. |
LAST_VALUE() | Returns the value of <expression> from the last row within the window frame. | [Required]: The column to compute. | All types. |
MIN() | Returns the minimum value of <expression> within the window frame. | [Required]: The column to compute. | Numeric, character, and time types. |
MAX() | Returns the maximum value of <expression> within the window frame. | [Required]: The column to compute. | Numeric, character, and time types. |
COUNT() | Returns the number of rows with non-NULL values of <expression> within the window frame. | [Required]: The column to compute. | All types. |
SUM() | Returns the sum of <expression> within the window frame. | [Required]: The column to compute. | Value type |
AVG() | Returns the average value of <expression> within the window frame. | [Required]: The column to compute. | Value type |
INT, BIGINT, FLOAT, DOUBLE, DECIMAL.CHAR, VARCHAR.DATE, TIME, DATETIME, TIMESTAMP.NTILE, PERCENT_RANK, CUME_DIST, and NTH_VALUE are not currently supported.DROP TABLE IF EXISTS test.tb_window;CREATE TABLE test.tb_window (c1 INT NOT NULL PRIMARY KEY,c2 INT,c3 INT);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 statementtdsql> SELECT c2, c3,COUNT(c1) OVER (PARTITION BY c2 ORDER BY c3ROWS BETWEEN CURRENT ROW AND 2 FOLLOWING) AS cnFROM 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 follows)| 1 | 6 | 1 | -- Window row index range: current (no data follows)+----+----+----+6 rows in set (0.06 sec)
-- Sample statementtdsql> SELECT c2, c3,COUNT(c1) OVER (PARTITION BY c2 ORDER BY c3RANGE BETWEEN CURRENT ROW AND 2 FOLLOWING) AS cnFROM test.tb_window;+----+----+----+| c2 | c3 | cn |+----+----+----+| 1 | 1 | 3 | -- Window range: rows corresponding to c3 in [1, 3] [0 ~ 2]| 1 | 1 | 3 || 1 | 2 | 2 | -- Window range: rows corresponding to c3 in [2, 4] [2 ~ 3]| 1 | 4 | 3 | -- Window range: rows corresponding to c3 in [4, 6] [3 ~ 5]| 1 | 6 | 2 | -- Window range: rows corresponding to c3 in [6, 8] [4 ~ 5]| 1 | 6 | 2 |+----+----+----+6 rows in set (0.06 sec)
<WindowFrame> restriction.-- Sample statementtdsql> SELECT c2, c3,ROW_NUMBER() OVER (PARTITION BY c2 ORDER BY c3) AS rnFROM 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)
RANK function: performs non-dense ranking on data within a partition and is not subject to the <WindowFrame> restriction.DENSE_RANK function: performs dense ranking on data within a partition and is not subject to the <WindowFrame> restriction.-- Sample statementSELECT c2, c3,RANK() OVER (PARTITION BY c2 ORDER BY c3) AS rk,DENSE_RANK() OVER (PARTITION BY c2 ORDER BY c3) AS 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)
LEAD(<expr>): returns the value of <expr> from the row following the current row within the partition, returns NULL when out of bounds, and is not subject to the <WindowFrame> restriction.LAG(<expr>): returns the value of <expr> from the row preceding the current row within the partition, returns NULL when out of bounds, and is not subject to the <WindowFrame> restriction.-- Sample statementtdsql> SELECT c2, c3,LEAD(c3) OVER (PARTITION BY c2 ORDER BY c3) AS ld,LAG(c3) OVER (PARTITION BY c2 ORDER BY c3) AS 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)
LEAD(<expr>, <offset>): returns the value of <expr> from the row that is <offset> rows after the current row within the partition, returns NULL when out of bounds, and is not subject to the <WindowFrame> restriction.LAG(<expr>, <offset>): returns the value of <expr> from the row that is <offset> rows before the current row within the partition, returns NULL when out of bounds, and is not subject to the <WindowFrame> restriction.-- Sample statementtdsql> SELECT c2, c3,LEAD(c3, 2) OVER (PARTITION BY c2 ORDER BY c3) AS ld,LAG(c3, 2) OVER (PARTITION BY c2 ORDER BY c3) AS 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)
LEAD(<expr>, <offset>, <default>): returns the value of <expr> from the row that is <offset> rows after the current row within the partition, returns <default> when out of bounds, and is not subject to the <WindowFrame> restriction.LAG(<expr>, <offset>, <default>): returns the value of <expr> from the row that is <offset> rows before the current row within the partition, returns <default> when out of bounds, and is not subject to the <WindowFrame> restriction.-- Sample statementtdsql> SELECT c2, c3,LEAD(c3, 2, 1000) OVER (PARTITION BY c2 ORDER BY c3) AS ld,LAG(c3, 2, 1000) OVER (PARTITION BY c2 ORDER BY c3) AS 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)
FIRST_VALUE(<expr>): returns the value of <expr> from the first row within the window frame.LAST_VALUE(<expr>): returns the value of <expr> from the last row within the window frame.-- Sample statementtdsql> SELECT c2, c3,FIRST_VALUE(c3) OVER (PARTITION BY c2 ORDER BY c3ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS fv,LAST_VALUE(c3) OVER (PARTITION BY c2 ORDER BY c3ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS 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)
MIN(<expr>): returns the minimum value of <expr> within the window frame.MAX(<expr>): returns the maximum value of <expr> within the window frame.-- Sample statementtdsql> SELECT c2, c3,MIN(c3) OVER (PARTITION BY c2 ORDER BY c3ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS mi,MAX(c3) OVER (PARTITION BY c2 ORDER BY c3ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS 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)
<expr> is not NULL.-- Sample statementtdsql> SELECT c2, c3,COUNT(c3) OVER (PARTITION BY c2 ORDER BY c3ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS cFROM 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)
<expr> within the window frame.-- Sample statementtdsql> SELECT c2, c3,SUM(c3) OVER (PARTITION BY c2 ORDER BY c3ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS sFROM 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)
<expr> within the window frame.-- Sample statementtdsql> SELECT c2, c3,AVG(c3) OVER (PARTITION BY c2 ORDER BY c3ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS aFROM 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)
Esta página foi útil?
Você também pode entrar em contato com a Equipe de vendas ou Enviar um tíquete em caso de ajuda.
comentários