Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1W8Fm6-0000Ai-IM for pgsql-bugs@arkaria.postgresql.org; Tue, 28 Jan 2014 21:00:50 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1W8Fm6-0001OK-1J for pgsql-bugs@arkaria.postgresql.org; Tue, 28 Jan 2014 21:00:50 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1W8Fm4-0001OD-Uv for pgsql-bugs@postgresql.org; Tue, 28 Jan 2014 21:00:49 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1W8Fm2-0004HQ-Fk for pgsql-bugs@postgresql.org; Tue, 28 Jan 2014 21:00:48 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id s0SL0iQU004456; Tue, 28 Jan 2014 16:00:45 -0500 From: Tom Lane To: markella.skempri@onzo.com cc: pgsql-bugs@postgresql.org Subject: Re: BUG #9010: partition by overrides order by in window functions In-reply-to: <20140128175229.8089.55168@wrigleys.postgresql.org> References: <20140128175229.8089.55168@wrigleys.postgresql.org> Comments: In-reply-to markella.skempri@onzo.com message dated "Tue, 28 Jan 2014 17:52:29 +0000" Date: Tue, 28 Jan 2014 16:00:44 -0500 Message-ID: <4455.1390942844@sss.pgh.pa.us> X-Pg-Spam-Score: -2.4 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org markella.skempri@onzo.com writes: > I am trying to get a row_number / rank of a resultset according to a value > that is ordered by date. However whenever I try to use the order by clause, > the partition by clause seems to override the ordering and produce false row > number. AFAICS, the behavior you're complaining about is exactly what is specified by the SQL standard. For one thing, since you've not specified any ORDER BY at the outer query level, there's no requirement for the rows to be returned in any particular order. But the main point is that the row_number is supposed to be computed within each set of rows having a distinct value of no_of_gap_days, which the actual output satisfies, and your wish-list result doesn't. So I'm pretty sure that you need to write something else than this in order to get the result you want. > What I expect to see > householdid previous_day gap_finish no_of_gap_days no_of_days > 1 2011-08-15 2011-08-16 1 1 > 1 2011-08-16 2011-08-17 1 2 > 1 2011-08-17 2011-08-18 1 3 > 1 2011-08-18 2011-08-19 1 4 > 1 2011-08-19 2011-08-20 1 5 > 1 2011-08-20 2011-08-21 1 6 > 1 2011-08-27 2011-08-28 1 7 > 1 2011-08-21 2011-08-27 6 1 > 1 2011-08-28 2011-08-29 1 1 > 1 2011-08-29 2011-08-30 1 2 Unfortunately, I can't help further, because it's not obvious to me what calculation rule you are wishing for. I could understand your example if it was ordered by date, but it isn't. regards, tom lane -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs