Re: Documentation, window functions

Поиск
Список
Период
Сортировка
От Bruce Momjian
Тема Re: Documentation, window functions
Дата
Msg-id 201102200502.p1K52Pg06003@momjian.us
обсуждение исходный текст
Ответ на Re: Documentation, window functions  (Robert Haas <robertmhaas@gmail.com>)
Список pgsql-hackers
Robert Haas wrote:
> On Thu, Sep 23, 2010 at 11:34 PM, Dennis Bj?rklund <db@zigo.dhs.org> wrote:
> >> On Wed, Sep 22, 2010 at 6:03 AM, Dennis Bj?rklund <db@zigo.dhs.org> wrote:
> >> But I confess that I'm sort of murky on how ORDER affects the window
> >> frame, or how to rephrase this more sensibly.
> >
> > The rows included in the calculation of the window function are per default
> >
> > RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
> >
> > where CURRENT ROW include all the rows that are equal to the row you are
> > at according to the ordering. So if you say order by name then all the
> > rows up to your name and all rows with the same name are included, not
> > later rows.
> >
> > If you don't have any ordering, then all rows are "equal" and all rows are
> > included in the computation. That's why your example behaved like it did.
> >
> > At least that's my understanding of how these things work. I've not used
> > window functions very much myself.
> >
> > This is fairly difficult stuff and it probably don't belong in a tutorial
> > but the current wording suggest that you can add any ordering and it won't
> > affect the result. That is also a bad since it might teach people the
> > wrong thing.
>
> Hmm... it is true that average will produce the same results on any
> ordering of the same set of input values, though.  Perhaps the word
> "partition" emcompass that, though then again maybe not.
>
> I'd be happy to fix this if I understand what to fix it to.

I clarified the window function ORDER BY wording to avoid mentioning
avg().  Applied patch attached.

--
  Bruce Momjian  <bruce@momjian.us>        http://momjian.us
  EnterpriseDB                             http://enterprisedb.com

  + It's impossible for everything to be true. +
diff --git a/doc/src/sgml/advanced.sgml b/doc/src/sgml/advanced.sgml
index 11859b4..218988e 100644
*** a/doc/src/sgml/advanced.sgml
--- b/doc/src/sgml/advanced.sgml
*************** SELECT depname, empno, salary, avg(salar
*** 383,392 ****
     </para>

     <para>
!     Although <function>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
!     <literal>ORDER BY</> within <literal>OVER</>.  Here is an example:

  <programlisting>
  SELECT depname, empno, salary, rank() OVER (PARTITION BY depname ORDER BY salary DESC) FROM empsalary;
--- 383,392 ----
     </para>

     <para>
!     You can also control the order in which rows are processed by
!     window functions using <literal>ORDER BY</> within <literal>OVER</>.
!     (The window <literal>ORDER BY</> does not even have to match the
!     order in which the rows are output.)  Here is an example:

  <programlisting>
  SELECT depname, empno, salary, rank() OVER (PARTITION BY depname ORDER BY salary DESC) FROM empsalary;

В списке pgsql-hackers по дате отправления:

Предыдущее
От: Robert Haas
Дата:
Сообщение: Re: FDW API: don't like the EXPLAIN mechanism
Следующее
От: Tom Lane
Дата:
Сообщение: Re: review: FDW API