官术网_书友最值得收藏!

  • Mastering PostgreSQL 9.6
  • Hans Jurgen Schonig
  • 164字
  • 2021-07-09 19:57:25

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 there? 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 provided by PostgreSQL is rank. It tells us:

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)

If somebody produced 9000 barrels per day, it would be the 27 best year in North America and 21 in the Middle East.

Note that in my example, I used NULLS LAST. When data is sorted, nulls are usually at the end. However, if sort order is reversed, nulls should still be at the end of the list. NULLS LAST ensures exactly that.
主站蜘蛛池模板: 浦江县| 五家渠市| 南投市| 余干县| 门源| 黑山县| 昂仁县| 新闻| 利辛县| 滨海县| 萝北县| 广德县| 稻城县| 巩义市| 闽侯县| 丰镇市| 临漳县| 长顺县| 伊春市| 得荣县| 怀远县| 大港区| 施甸县| 莱芜市| 山东| 宝鸡市| 景德镇市| 宜阳县| 安康市| 从化市| 双江| 齐齐哈尔市| 连江县| 玉屏| 蛟河市| 三台县| 靖远县| 乐陵市| 南澳县| 鹤峰县| 醴陵市|