- Mastering PostgreSQL 10
- Hans Jürgen Sch?nig
- 445字
- 2021-06-30 19:04:02
Ordering data inside a window
A PARTITION BY clause is not the only possible thing you can put into an OVER clause. Sometimes it is necessary to sort data inside a window. ORDER BY will provide data to your aggregate functions in a certain way. Here is an example:
test=# SELECT country, year, production, min(production) OVER (PARTITION BY country ORDER BY year) FROM t_oil WHERE year BETWEEN 1978 AND 1983 AND country IN ('Iran', 'Oman');
country | year | production | min ---------+-----+------------+------
Iran | 1978 | 5302 | 5302
Iran | 1979 | 3218 | 3218
Iran | 1980 | 1479 | 1479
Iran | 1981 | 1321 | 1321
Iran | 1982 | 2397 | 1321
Iran | 1983 | 2454 | 1321
Oman | 1978 | 314 | 314
Oman | 1979 | 295 | 295
Oman | 1980 | 285 | 285
Oman | 1981 | 330 | 285
...
Two countries (Iran and Oman) are chosen from our dataset for the period 1978 to 1983. Keep in mind, there was a revolution going on in Iran in 1979 so this had some impact on the production of oil. The data reflects that.
What the query does is to calculate the minimum production up to a certain point in our time series. At this point is a good way for SQL students to remember what an ORDER BY clause does inside an OVER clause. In this example, the PARTITION BY clause will create one group for each country and order data inside the group. The min function will loop over the sorted data and provide the required minimums.
If you are new to windowing functions, there is something you should be aware of: it really makes a difference whether you use an ORDER BY clause or not:
test=# SELECT country, year, production,
min(production) OVER (),
min(production) OVER (ORDER BY year)
FROM t_oil
WHERE year BETWEEN 1978 AND 1983
AND country = 'Iran';
country | year | production | min | min
---------+------+-------------+------+------
Iran | 1978 | 5302| 1321 | 5302
Iran | 1979 | 3218 | 1321 | 3218
Iran | 1980 | 1479 | 1321 | 1479
Iran | 1981 | 1321 | 1321 | 1321
Iran | 1982 | 2397 | 1321 | 1321
Iran | 1983 | 2454 | 1321 | 1321
(6 rows)
If the aggregate is used without ORDER BY it will automatically take the minimum of the entire dataset inside your windows. This doesn't happen if there is an ORDER BY. In this case, it will always be the minimum up to this point given the order you have defined.
- Managing Mission:Critical Domains and DNS
- 控制與決策系統(tǒng)仿真
- 手把手教你玩轉(zhuǎn)RPA:基于UiPath和Blue Prism
- 大數(shù)據(jù)挑戰(zhàn)與NoSQL數(shù)據(jù)庫(kù)技術(shù)
- Embedded Programming with Modern C++ Cookbook
- Ceph:Designing and Implementing Scalable Storage Systems
- PostgreSQL 10 Administration Cookbook
- Microsoft System Center Confi guration Manager
- 精通數(shù)據(jù)科學(xué):從線(xiàn)性回歸到深度學(xué)習(xí)
- AWS Administration:The Definitive Guide(Second Edition)
- 軟件需求最佳實(shí)踐
- Kubernetes Design Patterns and Extensions
- 網(wǎng)絡(luò)設(shè)備規(guī)劃、配置與管理大全(Cisco版)
- Mastering Kubernetes
- 傳感器技術(shù)及實(shí)訓(xùn)(第2版)