pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
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
Date: Wed, 07 May 2014 17:42:19 -0400
Message-ID: <5812.1399498939@sss.pgh.pa.us> (raw)
In-Reply-To: <20140507181702.1397.66739@wrigleys.postgresql.org>
References: <20140507181702.1397.66739@wrigleys.postgresql.org>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>

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



view thread (8+ messages)  latest in thread

Message-ID: <5812.1399498939@sss.pgh.pa.us>
Permalink:  ../5812.1399498939@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/5812.1399498939@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, arnaud.mouronval@gmail.com
  Subject: Re: BUG #10256: COUNT(*) behaves sort of like RANK() when used over a window containing an ORDER BY
  In-Reply-To: <5812.1399498939@sss.pgh.pa.us>
  References: <20140507181702.1397.66739@wrigleys.postgresql.org> <5812.1399498939@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