Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1W8F8l-0006wg-4B for pgsql-bugs@arkaria.postgresql.org; Tue, 28 Jan 2014 20:20:11 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1W8F8k-0002C6-Jz for pgsql-bugs@arkaria.postgresql.org; Tue, 28 Jan 2014 20:20:10 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1W8CqR-0007yw-1x for pgsql-bugs@postgresql.org; Tue, 28 Jan 2014 17:53:07 +0000 Received: from wrigleys.postgresql.org ([204.145.124.246]) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1W8CqN-0001Hw-DH for pgsql-bugs@postgresql.org; Tue, 28 Jan 2014 17:53:06 +0000 MIME-Version: 1.0 Subject: BUG #9010: partition by overrides order by in window functions To: pgsql-bugs@postgresql.org From: markella.skempri@onzo.com Date: Tue, 28 Jan 2014 17:52:29 +0000 Message-ID: <20140128175229.8089.55168@wrigleys.postgresql.org> X-Pg-Spam-Score: -1.9 (-) Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit 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 The following bug has been logged on the website: Bug reference: 9010 Logged by: Markella Skempri Email address: markella.skempri@onzo.com PostgreSQL version: 9.3.2 Operating system: Linux Centos 5.5 Description: 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. My data: householdid previous_day gap_finish no_of_gap_days 1 2011-08-15 2011-08-16 1 1 2011-08-16 2011-08-17 1 1 2011-08-17 2011-08-18 1 1 2011-08-18 2011-08-19 1 1 2011-08-19 2011-08-20 1 1 2011-08-20 2011-08-21 1 1 2011-08-21 2011-08-27 6 1 2011-08-27 2011-08-28 1 1 2011-08-28 2011-08-29 1 1 2011-08-29 2011-08-30 1 my query: select *, row_number() over (partition by no_of_gap_days order by gap_finish asc) as no_of_Days from temptable; the results: 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-28 2011-08-29 1 8 1 2011-08-29 2011-08-30 1 9 1 2011-08-21 2011-08-27 6 1 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 Thanks for your time -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs