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

Combining grouping sets with the FILTER clause

In real-world applications, grouping sets can often be combined with FILTER clauses. The idea behind the FILTER clause is to be able to run partial aggregates. Here is an example:

test=# SELECT region, 
avg(production) AS all, avg(production) FILTER (WHERE year < 1990) AS old, avg(production) FILTER (WHERE year >= 1990) AS new FROM t_oil GROUP BY ROLLUP (region);

region | all | old | new
---------------+----------------+----------------+----------------
Middle East | 1992.603686635 | 1747.325892857 | 2254.233333333
North America | 4541.362318840 | 4471.653333333 | 4624.349206349
| 2607.513986013 | 2430.685618729 | 2801.183150183 (3 rows)

The idea here is that not all columns will use the same data for aggregation. The FILTER clause allows you to selectively pass data to those aggregates. In this example, the second aggregate will only consider data before 1990, the third aggregate will take care of more recent data, and the first one will get all the data.

If it is possible to move conditions to a WHERE clause, it is always more desirable, as less data has to be fetched from the table. FILTER is only useful if the data left by the WHERE clause is not needed by each aggregate.

FILTER works for all kinds of aggregates and offers a simple way to pivot your data. Also, FILTER is faster than mimicking the same behavior with CASE WHEN ... THEN NULL ... ELSE END. You can find some real performance comparisons here: https://www.cybertec-postgresql.com/en/postgresql-9-4-aggregation-filters-they-do-pay-off/.

主站蜘蛛池模板: 大洼县| 南郑县| 朝阳市| 沙河市| 雅江县| 长沙县| 扬州市| 泸水县| 巢湖市| 肥西县| 乐至县| 阜新| 无锡市| 灵宝市| 漳平市| 新津县| 深水埗区| 龙游县| 孝昌县| 临沧市| 太保市| 西平县| 宣威市| 泉州市| 禄劝| 湾仔区| 万山特区| 武陟县| 伊金霍洛旗| 赣榆县| 通化市| 丹棱县| 奉节县| 沭阳县| 德令哈市| 松溪县| 东源县| 巴林右旗| 防城港市| 隆德县| 疏勒县|