pg.ddx.io pgsql-bugs@postgresql.org mailing list archive
help / color / mirror / Atom feedBUG #13918: Simple query with Having clause returns incorrect results
3+ messages / 3 participants
[nested] [flat]
* BUG #13918: Simple query with Having clause returns incorrect results
@ 2016-02-04 22:13 tarasbob@gmail.com
0 siblings, 1 reply; 3+ messages in thread
From: tarasbob@gmail.com @ 2016-02-04 22:13 UTC (permalink / raw)
To: pgsql-bugs
The following bug has been logged on the website:
Bug reference: 13918
Logged by: Taras Bobrovytsky
Email address: tarasbob@gmail.com
PostgreSQL version: 9.3.10
Operating system: Ubuntu
Description:
The following query incorrectly returns 1 row instead of 0:
SELECT 1
FROM some_table
WHERE FALSE
HAVING TRUE
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: BUG #13918: Simple query with Having clause returns incorrect results
@ 2016-02-09 02:45 David G. Johnston <david.g.johnston@gmail.com>
parent: tarasbob@gmail.com
0 siblings, 1 reply; 3+ messages in thread
From: David G. Johnston @ 2016-02-09 02:45 UTC (permalink / raw)
To: tarasbob@gmail.com <tarasbob@gmail.com>; +Cc: pgsql-bugs
On Thursday, February 4, 2016, <tarasbob@gmail.com> wrote:
> The following bug has been logged on the website:
>
> Bug reference: 13918
> Logged by: Taras Bobrovytsky
> Email address: tarasbob@gmail.com <javascript:;>
> PostgreSQL version: 9.3.10
> Operating system: Ubuntu
> Description:
>
> The following query incorrectly returns 1 row instead of 0:
>
> SELECT 1
> FROM some_table
> WHERE FALSE
> HAVING TRUE
>
>
>
So, amazingly (to me), this behavior is documented.
http://www.postgresql.org/docs/9.5/interactive/sql-select.html
"""
The presence of HAVING turns a query into a grouped query even if there is
no GROUP BY clause. This is the same as what happens when the query
contains aggregate functions but no GROUP BY clause. All the selected rows
are considered to form a single group, and the SELECT list and HAVING clause
can only reference table columns from within aggregate functions. Such a
query will emit a single row if the HAVINGcondition is true, zero rows if
it is not true.
"""
It doesn't matter that no rows are sourced your are guaranteed a single row
output if having evaluates to true. This is nice since you can do stuff
like counts and sums and get zeros for answers instead of dealing with an
empty result because nothing matched.
David J.
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: BUG #13918: Simple query with Having clause returns incorrect results
@ 2016-02-09 04:17 Tom Lane <tgl@sss.pgh.pa.us>
parent: David G. Johnston <david.g.johnston@gmail.com>
0 siblings, 0 replies; 3+ messages in thread
From: Tom Lane @ 2016-02-09 04:17 UTC (permalink / raw)
To: David G. Johnston <david.g.johnston@gmail.com>; +Cc: tarasbob@gmail.com <tarasbob@gmail.com>; pgsql-bugs
"David G. Johnston" <david.g.johnston@gmail.com> writes:
> On Thursday, February 4, 2016, <tarasbob@gmail.com> wrote:
>> The following query incorrectly returns 1 row instead of 0:
>>
>> SELECT 1
>> FROM some_table
>> WHERE FALSE
>> HAVING TRUE
> So, amazingly (to me), this behavior is documented.
>> The presence of HAVING turns a query into a grouped query even if there is
>> no GROUP BY clause. ... Such a
>> query will emit a single row if the HAVING condition is true, zero rows if
>> it is not true.
Yeah. This is a pretty weird corner case, but the behavior is required
by SQL spec.
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
^ permalink raw reply [nested|flat] 3+ messages in thread
end of thread, other threads:[~2016-02-09 04:17 UTC | newest]
Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2016-02-04 22:13 BUG #13918: Simple query with Having clause returns incorrect results tarasbob@gmail.com
2016-02-09 02:45 ` David G. Johnston <david.g.johnston@gmail.com>
2016-02-09 04:17 ` Tom Lane <tgl@sss.pgh.pa.us>
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