Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Wi9bb-0004wV-Tw for pgsql-bugs@arkaria.postgresql.org; Wed, 07 May 2014 21:42:24 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1Wi9bb-0001Uw-Ec for pgsql-bugs@arkaria.postgresql.org; Wed, 07 May 2014 21:42:23 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Wi9ba-0001Uq-PT for pgsql-bugs@postgresql.org; Wed, 07 May 2014 21:42:22 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Wi9bY-0001uB-Dj for pgsql-bugs@postgresql.org; Wed, 07 May 2014 21:42:22 +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 s47LgJuc005813; Wed, 7 May 2014 17:42:19 -0400 From: Tom Lane To: arnaud.mouronval@gmail.com cc: pgsql-bugs@postgresql.org Subject: Re: BUG #10256: COUNT(*) behaves sort of like RANK() when used over a window containing an ORDER BY In-reply-to: <20140507181702.1397.66739@wrigleys.postgresql.org> References: <20140507181702.1397.66739@wrigleys.postgresql.org> Comments: In-reply-to arnaud.mouronval@gmail.com message dated "Wed, 07 May 2014 18:17:02 -0000" Date: Wed, 07 May 2014 17:42:19 -0400 Message-ID: <5812.1399498939@sss.pgh.pa.us> X-Pg-Spam-Score: -2.6 (--) 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 arnaud.mouronval@gmail.com 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. > Here is a short SQL script to replicate it : > DROP TABLE IF EXISTS tmp_count_window_bug_data; > CREATE TABLE tmp_count_window_bug_data (c1 character varying(8), c2 > character varying(8)); > INSERT INTO tmp_count_window_bug_data (c1, c2) > VALUES > ('A', 'AA'), > ('A', 'AB'), > ('B', 'BA'), > ('B', 'BB'), > ('B', 'BC'), > ('B', 'BC'), > ('B', 'BD'); > SELECT > c1, > COUNT(*) OVER(PARTITION BY c1), > COUNT(*) OVER(PARTITION BY c1 ORDER BY c2) > FROM tmp_count_window_bug_data; > Result on my machine : > "A";2;1 > "A";2;2 > "B";5;1 > "B";5;2 > "B";5;4 > "B";5;4 > "B";5;5 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. 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