Oracle function records

1. Introduction of each function

1. OVER(PARTITION BY... ORDER BY...)--window function

1. The windowing function is used to define a window for the row (the window here refers to the set of rows to be operated by the operation), it operates on a set of values, does not need to use the GROUP BY clause to group the data, and can be used in the same Returns both the columns of the underlying row and the aggregated columns in row.

2. First divide a set of data into various groups according to the fields specified by (PARTITION BY), and then sort the groups according to a certain field. (ORDER BY);

2. The execution of grouping and sorting in OVER() is later than where, group by, and order by;

Common combinations:

 - row_number() over(partition by ... order by ...)		--分组排序
 - rank() over(partition by ... order by ...)		--分组排序
 - dense_rank() over(partition by ... order by ...)		--分组排序
 - count() over(partition by ... order by ...)		--分组计数
 - max() over(partition by ... order by ...) 		--分组取最大值
 - min() over(partition by    ... order by ...)		--分组取最小值
 - sum() over(partition by ... order by ...)		--分组求和
 - avg() over(partition by ... order by ...)		--分组取平均值
 - first_value() over(partition by ... order by ...)		--取分组第一条
 - last_value() over(partition by ... order by ...)			--取分组最后一条
 - lag() over(partition by ... order by ...)		--取出同一字段的前N行的数据
 - lead() over(partition by ... order by ...)		--取出同一字段的前N行的数据

2. Actual use

        row_number()over()

Let me talk about the requirements first: in the tree structure, only the first data information of the directory at the same level is obtained. The reason is that the branch and sub-branch information are at the same level. Here I only need the information of the branch, not the information of the sub-branch. The specific situation is shown in the following figure:

--目的:只获取同一级目录的第一个数据信息
SELECT JGBH,JGMC
FROM (
    --先根据父id进行分组,并将子id升序排序
  SELECT t.*, ROW_NUMBER() OVER (PARTITION BY SJJG ORDER BY JGBH) rn
  FROM GG_JGBH t
  where SYBZ = 1  --这是我筛选的条件,可更改
  CONNECT BY PRIOR JGBH = SJJG  --SJJG是父id,JGBH是子id
  START WITH SJJG IS NULL --从最高级的父id是空的开始
)
--再只获取每个分组的第一个数据
WHERE rn = 1;

         first_value()over()

first_value()over(partition by column name 1, column name 2 order by column name 1, column name 2) is to find the first value of a set of data

select distinct 
a.date,
a.name,
first_value(date)over(partition by name order by date asc)as `每个人对应最早的date`
,
first_value(date)over(partition by name order by date desc)as `每个人对应最晚的date`
from
(
select '张三'as name,'2021-04-11' as date
union all
select '李四'as name,'2021-04-09' as date
union all
select '赵四'as name,'2021-04-16' as date
union all
select '张三'as name,'2021-03-10'as date
union all
select '李四'as name,'2020-01-01'as date
)a


        last_value()over()

last_value()over(partition by column name 1, column name 2 order by column name 1, column name 2) is to find the last value of a set of data

last_value() defaults to ascending order, if it is restricted to descending order, it is equivalent to first_value() ascending order

select distinct a.date,a.name
,last_value(date)over(partition by name order by date asc)as `每个人对应最晚的date`
from
(
select '张三'as name,'2021-04-11' as date
union all
select '李四'as name,'2021-04-09' as date
union all
select '赵四'as name,'2021-04-16' as date
union all
select '张三'as name,'2021-03-10'as date
union all
select '李四'as name,'2020-01-01'as date
)a

select distinct a.date,a.name
,last_value(date)over(partition by name order by date rows between unbounded preceding and current row)as `(-∞,X)`
,last_value(date)over(partition by name order by date rows between unbounded preceding and unbounded following)as `(-∞,+ ∞)`
,last_value(date)over(partition by name order by date rows between current row and unbounded following)as `(X,+ ∞)`
from
(
select '张三'as name,'2021-04-11' as date
union all
select '李四'as name,'2021-04-09' as date
union all
select '赵四'as name,'2021-04-16' as date
union all
select '张三'as name,'2021-03-10'as date
union all
select '李四'as name,'2020-01-01'as date
)a

 

 

Guess you like

Origin blog.csdn.net/weixin_63610637/article/details/130169022