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

Utilizing windowing functions and analytics

After discussing ordered sets, it is time to take a look at windowing functions. Aggregates follow a fairly simple principle: take many rows and turn them into fewer, aggregated rows. A windowing function is different. It compares the current row with all rows in the group. The number of rows returned does not change.

Here is an example:

test=# SELECT avg(production) FROM t_oil; 
avg
-----------
2607.5139
(1 row)

test=# SELECT country, year, production, consumption, avg(production) OVER ()
FROM t_oil
LIMIT 4;
country | year | production | consumption | avg
---------+------+------------+-------------+----------
USA | 1965 | 9014 | 11522 | 2607.5139
USA | 1966 | 9579 | 12100 | 2607.5139
USA | 1967 | 10219 | 12567 | 2607.5139
USA | 1968 | 10600 | 13405 | 2607.5139
(4 rows)

The average production in our dataset is around 2.6 million barrels per day. The goal of this query is to add this value as a column. It is now easy to compare the current row to the overall average.

Keep in mind that the OVER clause is essential. PostgreSQL is not able to process the query without it:

test=# SELECT country, year, production, consumption, avg(production)  
FROM t_oil;
ERROR: column "t_oil.country" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: SELECT country, year, production, consumption, avg(productio...

This actually makes sense because the average has to be defined precisely. The database engine cannot just take any value, which might be right by doing guesswork.

Other database engines can accept aggregate functions without an  OVER or even a GROUP BY clause. However, from a logical point of view this is wrong and on top of that a violation of SQL.
主站蜘蛛池模板: 凤山县| 饶平县| 建瓯市| 容城县| 岳西县| 浦江县| 南靖县| 北安市| 葫芦岛市| 桦甸市| 龙南县| 安徽省| 安徽省| 益阳市| 古丈县| 姜堰市| 全南县| 香港| 长白| 太谷县| 盱眙县| 崇信县| 射洪县| 东乌珠穆沁旗| 三亚市| 霞浦县| 舟曲县| 嵊泗县| 兴海县| 夏津县| 文安县| 德安县| 八宿县| 都匀市| 西乌| 东至县| 西乡县| 五常市| 淮南市| 云梦县| 积石山|