Re: Documentation, window functions

From: Robert Haas <robertmhaas(at)gmail(dot)com>
To: Dennis Björklund <db(at)zigo(dot)dhs(dot)org>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: Documentation, window functions
Date: 2010-09-22 16:04:34
Message-ID: AANLkTikfMsWbEfYULAwJ=i3fnkpcDjX_q7=ZgAvPoaUo@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Wed, Sep 22, 2010 at 6:03 AM, Dennis Björklund <db(at)zigo(dot)dhs(dot)org> wrote:
> In
>
>  http://www.postgresql.org/docs/9.0/static/tutorial-window.html
>
> it say
>
> "Although avg will produce the same result no matter what order it
> processes the partition's rows in, this is not true of all window
> functions. When needed, you can control that order using ORDER BY within
> OVER."
>
> While it's true that avg() produce the same result no matter what order. A
> ORDER BY clause will affect what rows are included in the computation and
> thus change the result (the default window frame is
> RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW). So one can not in
> general add an ORDER BY to the example in the tutorial and get the same
> result as without an ORDER BY.
>
> Maybe we can find some better wording of the above?

Yeah, that doesn't seem right.

rhaas=# create table foo (a integer);
CREATE TABLE
rhaas=# insert into foo values (1);
INSERT 0 1
rhaas=# insert into foo values (2);
INSERT 0 1
rhaas=# insert into foo values (3);
INSERT 0 1
rhaas=# select a, avg(a) over () from foo;
a | avg
---+--------------------
1 | 2.0000000000000000
2 | 2.0000000000000000
3 | 2.0000000000000000
(3 rows)

rhaas=# select a, avg(a) over (order by a) from foo;
a | avg
---+------------------------
1 | 1.00000000000000000000
2 | 1.5000000000000000
3 | 2.0000000000000000
(3 rows)

But I confess that I'm sort of murky on how ORDER affects the window
frame, or how to rephrase this more sensibly.

--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise Postgres Company

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Bruce Momjian 2010-09-22 16:17:08 Re: Any reason why the default_with_oids GUC is still there?
Previous Message Robert Haas 2010-09-22 15:57:45 Re: Standby registration