- Mastering PostgreSQL 12
- Hans Jürgen Sch?nig
- 175字
- 2021-08-20 10:00:34
Understanding hypothetical aggregates
Hypothetical aggregates are pretty similar to standard ordered sets. However, they help to answer a different kind of question: what would be the result if a value was in the data? As you can see, this is not about values inside the database, but about the result if a certain value was actually there.
The only hypothetical function that's provided by PostgreSQL is rank, as shown in the following:
test=# SELECT region,
rank(9000) WITHIN GROUP
(ORDER BY production DESC NULLS LAST)
FROM t_oil
GROUP BY ROLLUP (1);
region | rank
---------------+------
Middle East | 21
North America | 27
| 47
(3 rows)
The preceding code tells us this: if somebody produced 9,000 barrels per day, it would be ranked the 27th best year in North America and 21st in Middle East.
In this example, I used NULLS LAST. When data is sorted, nulls are usually at the end. However, if the sort order is reversed, nulls should still be at the end of the list. NULLS LAST ensures exactly that.
推薦閱讀
- Unreal Engine:Game Development from A to Z
- Splunk 7 Essentials(Third Edition)
- 大數據項目管理:從規劃到實現
- PowerShell 3.0 Advanced Administration Handbook
- 計算機應用復習與練習
- Windows XP中文版應用基礎
- 大型數據庫管理系統技術、應用與實例分析:SQL Server 2005
- 完全掌握AutoCAD 2008中文版:綜合篇
- 數據掘金
- SAP Business Intelligence Quick Start Guide
- 教育機器人的風口:全球發展現狀及趨勢
- 基于RPA技術財務機器人的應用與研究
- 中老年人學電腦與上網
- 深度學習實戰
- Ubuntu 9 Linux應用基礎