sql常見開窗函數示例


1、開窗函數基本語法:

  函數名()over(partition by 分區字段 order by 排序字段)

2.1、示例數據:

2.2、示例sql:

create table kchs(
ID int,
product varchar(50),
amount decimal(18,2)
)

insert into kchs values (1,'蘋果',100);
insert into kchs values (2,'蘋果',200);
insert into kchs values (3,'蘋果',300);
insert into kchs values (4,'香蕉',450);
insert into kchs values (5,'香蕉',550);
insert into kchs values (6,'香蕉',650);
insert into kchs values (7,'西瓜',750);
insert into kchs values (8,'西瓜',850);
insert into kchs values (9,'西瓜',950);
insert into kchs values (10,'西瓜',950);
insert into kchs values (11,'西瓜',1050);

-- sum開窗,用於累計求和
select *,sum(amount)over(partition by product order by id) as sum_over from kchs order by id;

-- count開窗,用於統計當前分區當前行及以前行的紀錄數
select *,count(*)over(partition by product order by id) as count_over from kchs order by id;

-- max開窗,用於統計當前分區當前行及以前行的最大值
select *,max(amount)over(partition by product order by id) as max_over from kchs order by id;

-- min開窗,用於統計當前分區當前行及以前行的最小值
select *,min(amount)over(partition by product order by id) as min_over from kchs order by id;

-- avg開窗,用於統計當前分區當前行及以前行的平均數
select *,avg(amount)over(partition by product order by id) as avg_over from kchs order by id;

-- lag開窗,用於獲取往前偏移N行的數據
select *,lag(amount,1,null)over(partition by product order by id) as lag_over from kchs order by id;

-- lead開窗,用於獲取往后偏移N行的數據
select *,lead(amount,1,null)over(partition by product order by id) as lead_over from kchs order by id;

-- rank開窗,用於計算當前分區按排序規則的排名,會並列排名,排名不連續,比如並列第三名有兩個,那么就不會有第四名
select *,rank()over(partition by product order by amount) as rk from kchs order by id;

-- dense_rank開窗,用於計算當前分區按排序規則的排名,會並列排名,排名連續,比如並列第三名有兩個,那么仍然會有第四名
select *,dense_rank()over(partition by product order by amount) as drk from kchs order by id;

-- row_number開窗,用於計算當前分區按排序規則的排序,不會並列排序
select *,row_number()over(partition by product order by amount) as rn from kchs order by id;
View Code

3.1、sum開窗:

-- sum開窗,用於累計求和
select *,sum(amount)over(partition by product order by id) as sum_over from kchs order by id;

3.2、count開窗:

-- count開窗,用於統計當前分區當前行及以前行的紀錄數
select *,count(*)over(partition by product order by id) as count_over from kchs order by id;

 3.3、max開窗:

-- max開窗,用於統計當前分區當前行及以前行的最大值
select *,max(amount)over(partition by product order by id) as max_over from kchs order by id;

 3.4、min開窗:

-- min開窗,用於統計當前分區當前行及以前行的最小值
select *,min(amount)over(partition by product order by id) as min_over from kchs order by id;

 3.5、avg開窗:

-- avg開窗,用於統計當前分區當前行及以前行的平均數
select *,avg(amount)over(partition by product order by id) as avg_over from kchs order by id;

 3.6、lag開窗:

-- lag開窗,用於獲取往前偏移N行的數據
select *,lag(amount,1,null)over(partition by product order by id) as lag_over from kchs order by id;

 3.7、lead開窗:

-- lead開窗,用於獲取往后偏移N行的數據
select *,lead(amount,1,null)over(partition by product order by id) as lead_over from kchs order by id;

3.8、rank開窗:

-- rank開窗,用於計算當前分區按排序規則的排名,會並列排名,排名不連續,比如並列第三名有兩個,那么就不會有第四名
select *,rank()over(partition by product order by amount) as rk from kchs order by id;

 3.9、dense_rank開窗:

-- dense_rank開窗,用於計算當前分區按排序規則的排名,會並列排名,排名連續,比如並列第三名有兩個,那么仍然會有第四名
select *,dense_rank()over(partition by product order by amount) as drk from kchs order by id;

3.10、row_number開窗:

-- row_number開窗,用於計算當前分區按排序規則的排序,不會並列排序
select *,row_number()over(partition by product order by amount) as rn from kchs order by id;


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM