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

  • 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.
主站蜘蛛池模板: 阿荣旗| 南充市| 弥渡县| 晋江市| 崇义县| 新乡县| 长葛市| 达日县| 江油市| 定日县| 青川县| 松溪县| 宽甸| 文昌市| 泗水县| 西城区| 乌兰县| 北宁市| 泸水县| 安庆市| 杭锦后旗| 时尚| 揭东县| 故城县| 巫溪县| 永寿县| 阜阳市| 合山市| 阿拉尔市| 淳安县| 玉山县| 平南县| 米林县| 新绛县| 延津县| 牡丹江市| 潜江市| 鄂温| 山东| 和林格尔县| 乐亭县|