pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: wes@devauld.ca
Cc: pgsql-bugs@postgresql.org
Subject: Re: BUG #7685: last_value() not consistent throughout window partition
Date: Tue, 20 Nov 2012 12:47:25 -0500
Message-ID: <9922.1353433645@sss.pgh.pa.us> (raw)
In-Reply-To: <E1Tapjk-00074I-EU@wrigleys.postgresql.org>
References: <E1Tapjk-00074I-EU@wrigleys.postgresql.org>

wes@devauld.ca writes:
> -- The last_value(0 window function appears to be changing values
> -- mid window.  An example is detailed below.

So far as I can see, these results are correct per spec.  Note the
caution in the manual about these window functions:

  Note that first_value, last_value, and nth_value consider only the rows
  within the "window frame", which by default contains the rows from the
  start of the partition through the last peer of the current row. This is
  likely to give unhelpful results for last_value and sometimes also
  nth_value. You can redefine the frame by adding a suitable frame
  specification (RANGE or ROWS) to the OVER clause. See Section 4.2.8 for
  more information about frame specifications.

Since your d1 values are all distinct, the last peer is the same as the
current row, so last_value(v) ends up just producing the current row's v.
Which is what the query is producing.

If you want the last row in the partition, you could use first_value
with the opposite sort order, or use last_value with the same sort
order and RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING.
(The current SQL committee is nothing if not verbose :-()

			regards, tom lane




view thread (3+ messages)  latest in thread

Message-ID: <9922.1353433645@sss.pgh.pa.us>
Permalink:  ../9922.1353433645@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/9922.1353433645@sss.pgh.pa.us

 · 

reply

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: tgl@sss.pgh.pa.us, wes@devauld.ca
  Subject: Re: BUG #7685: last_value() not consistent throughout window partition
  In-Reply-To: <9922.1353433645@sss.pgh.pa.us>
  References: <E1Tapjk-00074I-EU@wrigleys.postgresql.org> <9922.1353433645@sss.pgh.pa.us>

* 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