ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

Using over() In SQL

2021-10-02 16:01:16  阅读:173  来源: 互联网

标签:passengers over year flights SQL Using order select


1. Introduction

In this article we will discuss over() function in SQL. over() is used in conjunction with other functions and mostly it means scatter the previous function's result over the original table rows.

 

2. The Data

We will use classical dataset flights.csv. It is each month passengers number between 1949 and 1960.

First we will copy the dataset into /tmp folder, which postgresql has permission to read.

$ cp flights.csv /tmp

Second we will create a new table in SQL and import data with copy.

create table flights (
	year int,
	month varchar,
	passengers int
);

copy flights
from '/tmp/flights.csv'
with csv header;

  

3. Ranking

To calculate the rank of a numeric column, we have rank() in SQL. But it has to work together with over().

We can use “order by” clause to specify ranking order.

select *, rank() over(order by passengers desc)
from flights;

We can use “partition by” clause to limit the calculation range only within each year.

Most of the time this is more reasonable method of ranking real world case.

select *, rank() over(partition by year order by passengers desc)
from flights;

There are also other kinds of ranking functions but that is another story. One common thing is they all have to work together with over().

select *, dense_rank() over(partition by year order by passengers desc)
from flights;

select *, row_number() over(partition by year order by passengers desc)
from flights;

 

4. Aggregation  

Normally an aggregate function will has a result with less row than the original table. But using over() we can scatter the result back to original table rows.

select *, sum(passengers) over()
from flights;

Using this trick we can do calculation between columns more easily.

select *, passengers / avg_yearly as floating_points
from (
	select *, avg(passengers) over(partition by year) as avg_yearly
	from flights
) as avg_cal
;

  

5. Cumsum(Cumulative Summary)

If we want to do cumsum in SQL, we will still have to use sum() with over(). With the “order by” clause we can specify the cumulating order. And with the “partition by” clause we can limit the culmulating calculation only within a range.

select *, sum(passengers) over(partition by year order by month)
from flights;

 

6. Moving Average(Rolling Mean)  

Last topic today of over() is moving average. We can use a special grammar “rows between ... and ... row” to specify summary range. This can be used as moving average.

select *, avg(passengers) over(partition by year rows between 2 preceding and current row)
from flights;

  

  

 

标签:passengers,over,year,flights,SQL,Using,order,select
来源: https://www.cnblogs.com/drvongoosewing/p/15314404.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有