ORACLE database (6)-window function

ORACLE database (6)-window function

Window function, also known as analytical function, window function, OLAP (data analysis) function

Aggregation function: group data according to certain rules, analyze a certain situation of each group uniformly, and return a row of results for each grouping.
Window function: group data according to certain rules, and analyze a certain situation of each group uniformly.每行数据返回一行结果

One, window function syntax

Analysis function name () over (analysis clause)

over作为开窗函数的标志

Second, the analysis clause

Grouping (PARTITION BY) Sorting (ORDER BY) Window (ROWS)

Three, window conditions

PRECEDING: previous

FOLLOWING: Later

CURRENT: current

UNBOUNDING: Unrestricted

ROW: Row

ROWS BETWEEN N PRECEDING AND N FOLLOWING -- 前面N行---- 后面N行
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW  -- 第一行 - 当前行
ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING  -- 当前行 - 最后一行
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING -- 第一行 - 最后一行

Three, analysis function

1 Aggregate class

SUM(), AVG(), MAX(), MIN(), COUNT() have the same functions as aggregate functions

1 PARTITION BY 默认窗口的作用范围是从每个组的第一行到最后一行

2 ORDER BY 默认窗口作用范围是从第一条到当前条

3 ORDER BY 分组内部排序,尽可能的按照顺序排,但是结果不一定完全按照内部排序结果

Example with SUM()

SUM(列名) OVER([PARTITION] [ORDER] [ROWS])

--查询每天的营业额及周每日累计营业额
SELECT DATA_DT,AMT,SUM(AMT)OVER(PARTITION BY WEEK ORDER BY DATA_DT ASC) FROM BUSINESS; --  各组内的第一条到当前条

Insert picture description here
important point

1) The analysis function name must contain the content that needs to be analyzed.
2) The analysis clause has no hard requirements --- ORDER BY must be accompanied when ROWS appears.
3) When the default window range is used, the next N is the same value (the value used for sorting is the same ) Will be included
2 Sorting class

ROW_NUMBER: The ranks of rows with the same sort value are not tied or jumped

RANK: rank the rows with the same sort value in parallel and jump

DENSE_RANK: Do not jump parallel rows with the same sort value

usage

ROW_NUMBER()OVER([PARTITION BY ] ORDER BY)

查询每天的营业额并在整月范围内升序排列

SELECT DATA_DT,AMT,ROW_NUMBER()OVER(ORDER BY AMT) RN FROM BUSINESS; -- 不并列 不跳跃
SELECT DATA_DT,AMT,RANK()OVER(ORDER BY AMT) RN FROM BUSINESS; -- 并列跳跃
SELECT DATA_DT,AMT,DENSE_RANK()OVER(ORDER BY AMT) RN FROM BUSINESS; -- 并列不跳跃

Insert picture description here

Insert picture description here

Insert picture description here

ROW_NUMBER 可以用来去除重复的行 完全重复和不完全重复(一般多条数据中除了日期不同其他均相同的行)

Points to note for sorting window functions:
1) The name of the molecular function cannot contain any content
2) ORDER BY must be added in the analysis clause, and the window cannot be specified
3 Offset class

LAG(): offset forward

LEAD(): offset backward

usage

Usage (take LAG as an example): LAG(COL_NAME,[OFFSET],[DEFVAL])OVER(): Offset forward by N lines to take the number
COL_NAME: Field to be analyzed
OFFSET: Offset-default offset one line
DEFVAL : Default return value-null is returned by default. The return type should be the same as the type of the analysis field

--查询每天的营业额以及前一天的营业额
SELECT DATA_DT,AMT,
       LAG(AMT,1,0)OVER(ORDER BY DATA_DT)
  FROM BUSINESS;

Insert picture description here

Points to note for the offset windowing function:
1) The analysis function name must contain the content to be analyzed, and the other two parameters can be defaulted.
2) ORDER BY must be added in the analysis clause, and the window cannot be specified.
3) If there is no more For the offset row, the default value is returned
4) The offset is not allowed to write negative numbers
5) The default return value data type should be consistent with the analyzed field

Four, summary

1 The window clause cannot appear alone, and the window range can be specified only
when there is a sorting clause. 2 If an ORDER BY appears and the window range is not specified at the same time, the default window range is from the first row to the current row ; If the sort clause does not appear, and the window range is not specified at the same time, the default window range is the first row to the last row

Guess you like

Origin blog.csdn.net/c727657851/article/details/115015989