Re: Windowing functions vs aggregates - Mailing list pgsql-hackers

From Tom Lane
Subject Re: Windowing functions vs aggregates
Date
Msg-id 9505.1239717509@sss.pgh.pa.us
Whole thread Raw
In response to Re: Windowing functions vs aggregates  (Greg Stark <stark@enterprisedb.com>)
List pgsql-hackers
Greg Stark <stark@enterprisedb.com> writes:
> However, I'm kind of confused by that result. Why does the range
> "between unbounded preceding and current row" seem to be doing the
> average of the whole result set?

That's what it's supposed to do. "Current row" really includes all peers
of the current row in the window frame ordering, and since you didn't
specify any ORDER BY clause, all the rows are peers.  If you put in
"order by s" you'll get the result you were expecting:

regression=# select s,(avg(s) OVER (range between unbounded preceding
and current row)) from foo;s |        avg         
---+--------------------1 | 2.50000000000000002 | 2.50000000000000003 | 2.50000000000000004 | 2.5000000000000000
(4 rows)

regression=# select s,(avg(s) OVER (order by s range between unbounded preceding                          
and current row)) from foo;s |          avg           
---+------------------------1 | 1.000000000000000000002 |     1.50000000000000003 |     2.00000000000000004 |
2.5000000000000000
(4 rows)


I suppose the SQL committee defined it like that to try to reduce the
implementation dependency of the results.
        regards, tom lane


pgsql-hackers by date:

Previous
From: Tom Lane
Date:
Subject: Re: Windowing functions vs aggregates
Next
From: Marko Kreen
Date:
Subject: Re: Unicode string literals versus the world