視窗函式之range的用法(讀書筆記三)

2021-06-25 12:18:46 字數 3845 閱讀 7348

--range的具體用法

select

emp_id,emp_name,dept_id,hire_date,salary,

--首先按dept_id進行分組,其次按照hire_date進行排序,然後再把入職時間小於等於自身的所有員工薪資進行累計

sum(salary) over(partition by dept_id order by hire_date) sum_salary_part_order,

--按照dept_id進行分組,並統計該部門下所有員工的薪資

sum(salary) over(partition by dept_id) sum_salary_part,

--直接按照hire_date進行分組,再把入職時間小於等於自身的所有員工薪資進行累計

sum(salary) over(order by hire_date) sum_salary_order,

--後面均為首先按dept_id進行分組,其次按照hire_date進行排序,且所有統計不能跨越其所在分割槽,故不再重複

--且由於是邏輯範圍,因此在preceding和following的表示式上是有符號的

--視窗範圍為該分割槽的第一行和該分割槽的最後一行,與sum_salary_part等同,在非條件表示式中等同於rows

sum(salary) over(partition by dept_id order by hire_date

range between unbounded preceding and unbounded following) sum_1,

--視窗範圍為該分割槽的第一行和本行,與sum_salary_part_order等同,在非條件表示式中等同於rows

sum(salary) over(partition by dept_id order by hire_date

range between unbounded preceding and current row) sum_2,

--視窗範圍為該分區內小於本記錄hire_date-365天的所有的薪資累計

sum(salary) over(partition by dept_id order by hire_date

range between unbounded preceding and 365/*value_expr*/ preceding) sum_3,

--視窗範圍為該分區內小於本記錄hire_date+365天的所有的薪資累計

sum(salary) over(partition by dept_id order by hire_date

range between unbounded preceding and 365/*value_expr*/ following) sum_4,

--視窗範圍為本行和該分割槽的最後一行,統計的是大於等於本記錄hire_date之後的所有薪資,在非條件表示式中等同於rows

sum(salary) over(partition by dept_id order by hire_date

range between current row and unbounded following) sum_5,

--視窗範圍是本行,所以與本行薪資一樣

sum(salary) over(partition by dept_id order by hire_date

range between current row and current row) sum_6,

--視窗範圍為該分區內本記錄起和小於本記錄hire_date+365天的所有的薪資累計

sum(salary) over(partition by dept_id order by hire_date

range between current row and 365/*value_expr*/ following) sum_7,

--視窗範圍為該分區內大於本記錄hire_date—365天和該分割槽最後一行的所有薪資累計

sum(salary) over(partition by dept_id order by hire_date

range between 365/*value_expr*/ preceding and unbounded following) sum_8,

--視窗範圍為該分區內大於本記錄hire_date-365天,並且截止到當前記錄的所有的薪資累計

sum(salary) over(partition by dept_id order by hire_date

range between 365/*value_expr*/ preceding and current row) sum_9,

--視窗範圍為該分割槽的本行current_value-value_expr1到本行current_value-value_expr2的累計,強調value_expr1>=value_expr2

sum(salary) over(partition by dept_id order by hire_date

range between 365/*value_expr1*/ preceding and 30/*value_expr2*/ preceding) sum_10,

--視窗範圍為該分割槽的本行current_value-value_expr1到本行current_value+value_expr2的累計

sum(salary) over(partition by dept_id order by hire_date

range between 365/*value_expr*/ preceding and 30/*value_expr*/ following) sum_11,

--視窗範圍為該分區內大於本記錄hire_date+365天的所有的薪資累計

sum(salary) over(partition by dept_id order by hire_date

range between 365/*value_expr*/ following and unbounded following) sum_12,

--視窗範圍為該分割槽的本行current_value+value_expr1到本行current_value+value_expr2的累計,本例為本行後1行和後2行的累計,強調value_expr1<=value_expr2

sum(salary) over(partition by dept_id order by hire_date

range between 30/*value_expr1*/ following and 365/*value_expr2*/ following) sum_13,

--視窗範圍為該分割槽的第一行,結束行預設本行,與sum_salary_part_order,sum_2等同,在非條件表示式中等同於rows

sum(salary) over(partition by dept_id order by hire_date

range unbounded preceding) sum_14,

--視窗範圍僅為當前行,所以與本行薪資一樣,與sum_6一樣,在非條件表示式中等同於rows

sum(salary) over(partition by dept_id order by hire_date

range current row) sum_15,

--視窗範圍為該分區內大於本記錄hire_date-365天,並且截止到當前記錄的所有的薪資累計,與sum_9等價

sum(salary) over(partition by dept_id order by hire_date

range 365/*value_expr*/ preceding) sum_16

from employeeinfo

order by dept_id,hire_date;

range 函式用法

python range 函式可建立乙個整數列表,一般用在 for 迴圈中。函式語法 range start,stop step 引數說明 start 計數從 start 開始。預設是從 0 開始。例如range 5 等價於range 0,5 stop 計數到 stop 結束,但不包括 stop。例...

Python的range 函式用法

使用python的人都知道range 函式很方便,今天再用到他的時候發現了很多以前看到過但是忘記的細節。這裡記錄一下range 複習下list的slide,最後分析乙個好玩兒的冒泡程式。這裡記錄一下 range 1,5 代表從1到5 不包含5 1,2,3,4 range 1,5,2 代表從1到5,間...

視窗函式之rows的用法 讀書筆記二

rows的具體用法 select emp id,emp name,dept id,hire date,salary,首先按dept id進行分組,其次按照hire date進行排序,然後再把入職時間小於等於自身的所有員工薪資進行累計 sum salary over partition by dept...