From: David G Johnston <david.g.johnston@gmail.com>
To: pgsql-bugs@postgresql.org
Subject: Re: BUG #10256: COUNT(*) behaves sort of like RANK() when used over a window containing an ORDER BY
Date: Wed, 7 May 2014 16:08:23 -0700 (PDT)
Message-ID: <1399504103854-5803103.post@n5.nabble.com> (raw)
In-Reply-To: <5812.1399498939@sss.pgh.pa.us>
References: <20140507181702.1397.66739@wrigleys.postgresql.org>
<5812.1399498939@sss.pgh.pa.us>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>
Tom Lane-2 wrote
> arnaud.mouronval@
> writes:
>> I discovered a problem while using a window that used an ORDER BY clause,
>> and using this window with ROW_NUMBER() and COUNT(*) at the same time.
>
> This looks correct to me. Keep in mind that the default window framing
> clause is RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW, which really
> means "all rows up to and including the last sort peer of the current
> row".
> With no ORDER BY clause, all rows in the partition are peers, so you get a
> count covering the whole partition no matter which row you are on. With
> an ORDER BY, you get a count of only the rows up to the current row (or
> its last peer, when there are duplicate values in the ORDER BY columns).
>
>> I was expecting to get the same values in the last 2 columns.
>
> For that you'd want RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED
> FOLLOWING.
Tom's quote is pretty much an abbreviation of the following taken directly
from:
http://www.postgresql.org/docs/9.3/interactive/sql-select.html
"The default framing option is RANGE UNBOUNDED PRECEDING, which is the same
as RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW. With ORDER BY, this
sets the frame to be all rows from the partition start up through the
current row's last peer. Without ORDER BY, all rows of the partition are
included in the window frame, since all rows become peers of the current
row."
A more useful description and example is found in the tutorial (about
halfway down, the sum(*) example):
http://www.postgresql.org/docs/9.3/interactive/tutorial-window.html
The term "peer" in the first quotation is confusing to me. My understanding
is that "PARTITION BY" defines which rows are "peers" - even if that isn't
the wording used. So now using "peers" in relation to the FRAME clause
confuses the issue. IMO the "since all rows..." is really superfluous - I
would understand:
"In the presence of an ORDER BY only rows up to the current row (including
all [partition] peers) are considered. Omitting an ORDER BY causes the
entire PARTITION to be considered the FRAME. Other frame definitions can be
requested by supplying an explicit FRAME clause."
I knew the answer but in the interest of looking through the user's eyes I
wanted to go take a look (again, done this before) and really am not
surprised that these kinds of questions are being asked. Just having to
scan through 5 sections of the documentation is difficult. That said, the
tutorial section and inline examples make it quite clear how the
with/without ORDER BY behavior of window functions (sum in the example)
cause different results.
At minimum the top of 9.3.4 could provide links to, and
descriptions/summaries of, what the other 4 sections cover and why things
are broken out the way they are. The other cross-references could point
back to that section-subsection as a kind of launch point: "Please see
section 3.5.1 for an overview of, and links to, other related sections."
Just some food for thought if anyone is industrious and annoyed enough to
act on it.
David J.
--
View this message in context: http://postgresql.1045698.n5.nabble.com/BUG-10256-COUNT-behaves-sort-of-like-RANK-when-used-over-a-w...
Sent from the PostgreSQL - bugs mailing list archive at Nabble.com.
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-bugs@postgresql.org
Cc: david.g.johnston@gmail.com
Subject: Re: BUG #10256: COUNT(*) behaves sort of like RANK() when used over a window containing an ORDER BY
In-Reply-To: <1399504103854-5803103.post@n5.nabble.com>
References: <20140507181702.1397.66739@wrigleys.postgresql.org> <5812.1399498939@sss.pgh.pa.us> <1399504103854-5803103.post@n5.nabble.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox