Using over() In SQL
2021/10/2 19:10:58
本文主要是介绍Using over() In SQL,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
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;
这篇关于Using over() In SQL的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-07-04TiDB 资源管控的对撞测试以及最佳实践架构
- 2024-07-03万字长文聊聊Web3的组成架构
- 2024-07-02springboot项目无法注册到nacos-icode9专业技术文章分享
- 2024-06-26结对编程到底难不难?答案在这里
- 2024-06-19《2023版Java工程师》课程升级公告
- 2024-06-15matplotlib作图不显示3D图,怎么办?
- 2024-06-1503-Loki 日志监控
- 2024-06-1504-让LLM理解知识 -Prompt
- 2024-06-05做软件测试需要懂代码吗?
- 2024-06-0514-ShardingSphere的分布式主键实现