You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
BETWEEN {UNBOUNDED PRECEDING | <expr> PRECEDING | <expr> FOLLOWING | CURRENT ROW} AND
36
36
{UNBOUNDED FOLLOWING | <expr> PRECEDING | <expr> FOLLOWING | CURRENT ROW}
37
37
38
+
<window frame exclusion> ::=
39
+
EXCLUDE {NO OTHERS | CURRENT ROW | GROUP | TIES}
40
+
38
41
<direction> ::=
39
42
{ASC | DESC}
40
43
@@ -279,6 +282,28 @@ With `ROWS`, order expressions is not limited by number or types. In this case,
279
282
280
283
The frame syntax with `<window frame start>` specifies the start frame, with the end frame being `CURRENT ROW`.
281
284
285
+
The optional frame exclusion clause (FB 6.0) removes rows from the frame after its bounds have been evaluated. `EXCLUDE NO OTHERS` is the default and keeps the frame unchanged.
286
+
287
+
`EXCLUDE CURRENT ROW` removes only the current row from the frame.
288
+
289
+
`EXCLUDE GROUP` removes the current row and all its peers. Peers are rows with the same `ORDER BY` values as the current row. If there is no `ORDER BY`, all rows in the partition are peers.
290
+
291
+
`EXCLUDE TIES` removes the peers of the current row, but keeps the current row itself. If there is no `ORDER BY`, all other rows in the partition are removed from the frame.
292
+
293
+
For example, with duplicate salaries, the following query sums the full ordered partition except the current salary peer group:
294
+
295
+
```sql
296
+
select
297
+
id,
298
+
salary,
299
+
sum(salary) over (
300
+
order by salary
301
+
rows between unbounded preceding and unbounded following
302
+
exclude group
303
+
) sum_other_salaries
304
+
from employee
305
+
order by salary;
306
+
```
282
307
283
308
284
309
When `ORDER BY` window clause is used but frame clause is omitted, it defaults to `RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW`. This fact makes the query below to produce "weird" behaviour for the "sum_salary" column. It sums from the partition start to the current key, instead of sum the whole partition.
@@ -346,9 +371,9 @@ select
346
371
| 5 | 10.00 | 3 |
347
372
| 2 | 12.00 | 1 |
348
373
349
-
Some window functions discard frames. `ROW_NUMBER`, `LAG` and `LEAD` always work as `ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW`. And `DENSE_RANK`, `RANK`, `PERCENT_RANK` and `CUME_DIST` always work as `RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW`.
374
+
Some window functions discard frames and frame exclusions. `ROW_NUMBER`, `LAG` and `LEAD` always work as `ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW`. And `DENSE_RANK`, `RANK`, `PERCENT_RANK` and `CUME_DIST` always work as `RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW`.
350
375
351
-
`FIRST_VALUE`, `LAST_VALUE` and `NTH_VALUE` respect frames, but the `RANGE` unit works identically as `ROWS`.
376
+
`FIRST_VALUE`, `LAST_VALUE` and `NTH_VALUE` respect frames and frame exclusions, but the `RANGE` unit works identically as `ROWS`.
0 commit comments