Received: from localhost (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 8BFE26332C4 for ; Mon, 12 Oct 2009 04:10:15 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 42022-05 for ; Mon, 12 Oct 2009 07:10:01 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-fx0-f225.google.com (mail-fx0-f225.google.com [209.85.220.225]) by mail.postgresql.org (Postfix) with ESMTP id 57223633260 for ; Mon, 12 Oct 2009 04:10:04 -0300 (ADT) Received: by fxm25 with SMTP id 25so8399815fxm.24 for ; Mon, 12 Oct 2009 00:10:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :from:date:message-id:subject:to:content-type; bh=q7Dhax5UYopDGUqPRVeA8o5R/r7WFi0Hm4cmmUPq940=; b=sFNdtFHWMsSRwDGWxWytv5j+HBPKIyD7pGjarhsHdU/54wTd/dYr0JE17cwfabkoRX lQsdjhbGqEe8cR71btV8m5y0II8V9Z1gqVcnc3DOxJpqLc9HK8Kn9qjLxrZJ9SiYmrV/ au9Iwmky7Bt3Wjjkb2IrRUMBfgeKWh+kxzIRc= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; b=g5NXuX31c4e6p9ZZNme6+6KrXjepqObjxDKcdfpF+WiUHOs4s0gQYjYnN9TmAFgcHK oElzITsiuU5axBm5OfvRBLKQ2MYeFPwtEd2aRkydslabHmPJ0u7cYfH0mHaaD1rwY4HV oCnhjeyHDhGCjFMUIwVVwAY08kiA52ZrN4CT8= MIME-Version: 1.0 Received: by 10.223.4.148 with SMTP id 20mr1489010far.0.1255331402269; Mon, 12 Oct 2009 00:10:02 -0700 (PDT) In-Reply-To: References: From: Thom Brown Date: Mon, 12 Oct 2009 08:09:42 +0100 Message-ID: Subject: Re: Lag and lead window functions order by weirdness To: pgsql-sql@postgresql.org Content-Type: multipart/alternative; boundary=000e0ce044b44ec48b0475b79e0b X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.543 tagged_above=-10 required=5 tests=AWL=0.055, BAYES_00=-2.599, HTML_MESSAGE=0.001 X-Spam-Level: X-Archive-Number: 200910/64 X-Sequence-Number: 33263 --000e0ce044b44ec48b0475b79e0b Content-Type: text/plain; charset=ISO-8859-1 2009/10/10 Thom Brown > I've had a look at examples of lag and lead window functions with order by > inside the OVER clause, and I'm confused as to why it influences the overall > order in the output. > For example (for a table called category with incrementing id numbers up to > 26): > > SELECT id, lag(id) OVER (ORDER BY id DESC) FROM category; > > This would yield: > > id lag > 26 NULL > 25 26 > 24 25 > > This is the equivalent of what we would get with: > SELECT id, lead(id) OVER (ORDER BY id ASC) FROM category ORDER BY id DESC; > > I would expect the row order in the results not to be influenced by the > OVER clause's ORDER BY as I thought that was just to determine how the > aggregate window function's values would be output. It's almost as if the > main part of the query has inherited it's order from the OVER clause. > > Am I missing something here? > > Thanks > > Thom > Having a look around, it looks as if Postgres might be misbehaving. According to this page, http://my.safaribooksonline.com/0596004818/sqlnut2-CHP-4-SECT-3, the ORDER BY in the window function's clause shouldn't be having this ordering effect: "Furthermore, the order within these groups is defined by an ordering clause, but that order only affects function evaluation, and has no effect on the order in which rows are returned by the query." The behaviour is unexpected from my perspective, but obviously there are workarounds. Is anyone able to confirm any of this? Thom --000e0ce044b44ec48b0475b79e0b Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
2009/10/10 Thom Brown <thombrown@gmail.com>
I've had a look at examples of lag and lead window functions with order= by inside the OVER clause, and I'm confused as to why it influences th= e overall order in the output.

For example (for a table = called category with incrementing id numbers up to 26):

SELECT id, lag(id) OVER (ORDER BY id DESC) FROM categor= y;

This would yield:

id = =A0 lag
26 =A0 NULL
25 =A0 26
24 =A0 25
=

This is the equivalent of what we would get with:
= SELECT id, lead(id) OVER (ORDER BY id ASC) FROM category ORDER BY id DESC;<= /div>

I would expect the row order in the results not to= be influenced by the OVER clause's ORDER BY as I thought that was just= to determine how the aggregate window function's values would be outpu= t. =A0It's almost as if the main part of the query has inherited it'= ;s order from the OVER clause.

Am I missing something here?

T= hanks

Thom

Having a look around, it looks as if Pos= tgres might be misbehaving. =A0According to this page,=A0http://my.safarib= ooksonline.com/0596004818/sqlnut2-CHP-4-SECT-3, the ORDER BY in the win= dow function's clause shouldn't be having this ordering effect:

"Furthermore, the order within these groups is def= ined by an ordering clause, but that order only affects function evaluation= , and has no effect on the order in which rows are returned by the query.&q= uot;

The behaviour is unexpected from my perspective, but ob= viously there are workarounds. =A0Is anyone able to confirm any of this?

Thom
--000e0ce044b44ec48b0475b79e0b--