// window reference

PostgreSQL window function features

PostgreSQL 11+ has FILTER, GROUPS, EXCLUDE, RANGE offsets and named windows; the notable absences are IGNORE NULLS, FROM LAST and QUALIFY.

カテゴリ
ウィンドウ
Returns
per-frame aggregate
In this category
74 of 79

構文

AVG(x) FILTER (WHERE c) OVER (ORDER BY d GROUPS BETWEEN 1 PRECEDING AND CURRENT ROW EXCLUDE TIES)

エンジンサポート

エンジンSupportメモ
SQL Server Not built in No built-in equivalent.
PostgreSQL はい 11+ for GROUPS/EXCLUDE/RANGE offsets; 9.4+ for FILTER; no IGNORE NULLS or QUALIFY through PostgreSQL 18
MySQL Not built in No built-in equivalent.
SQLite Not built in No built-in equivalent.
Oracle Not built in No built-in equivalent.

例

PostgreSQL

SELECT symbol, day, close,
       AVG(close) FILTER (WHERE volume > 0) OVER w AS avg_active,
       AVG(close) OVER (w ORDER BY day GROUPS BETWEEN 2 PRECEDING AND CURRENT ROW
                        EXCLUDE CURRENT ROW) AS ma3_ex_self
FROM quotes
WINDOW w AS (PARTITION BY symbol);

知っておくと便利なこと

  • Still missing through PostgreSQL 18: IGNORE NULLS and FROM LAST on the offset functions, and QUALIFY. The COUNT/MAX forward fill and the CTE filter replace them.
  • DISTINCT inside window aggregates and ORDER BY inside a windowed aggregate call (string_agg(x, ',' ORDER BY x) OVER ...) both raise 'not implemented for window functions'.
  • Named windows can be extended (OVER (w ORDER BY ...)) as long as w has no ORDER BY or frame of its own, and FILTER combines freely with frames and EXCLUDE.

Questions about PostgreSQL Window Features

Does PostgreSQL support IGNORE NULLS?

No, through PostgreSQL 18 it is a syntax error. Forward-fill with the COUNT(expr) OVER (ORDER BY id) grouping trick or a LATERAL last-non-NULL join.

Does PostgreSQL implement the whole standard for windows?

Most of the frame syntax (GROUPS, EXCLUDE, RANGE offsets, named windows) is there. The gaps are IGNORE NULLS, FROM LAST, DISTINCT in window aggregates and aggregate ORDER BY in window form. QUALIFY is not standard, so its absence is not a gap.

Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.

Cheat sheets for the engines that have it: PostgreSQL.

// fewer lookups

Functions your engine actually has

SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.