From: | "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com> |
---|---|
To: | "tarasbob(at)gmail(dot)com" <tarasbob(at)gmail(dot)com> |
Cc: | "pgsql-bugs(at)postgresql(dot)org" <pgsql-bugs(at)postgresql(dot)org> |
Subject: | Re: BUG #13918: Simple query with Having clause returns incorrect results |
Date: | 2016-02-09 02:45:56 |
Message-ID: | CAKFQuwYSa5Dzvw8KdxhiUAY+fjbO4DRQ-sDqQXPVexvVoTkvQA@mail.gmail.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-bugs |
On Thursday, February 4, 2016, <tarasbob(at)gmail(dot)com> wrote:
> The following bug has been logged on the website:
>
> Bug reference: 13918
> Logged by: Taras Bobrovytsky
> Email address: tarasbob(at)gmail(dot)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.
From | Date | Subject | |
---|---|---|---|
Next Message | David G. Johnston | 2016-02-09 03:28:43 | Re: BUG #13920: pg_try_advisory_xact_lock bigint trouble |
Previous Message | David G. Johnston | 2016-02-09 02:23:52 | Re: BUG #13937: 'src' -> jsonb_each() -> jsonb_object() -> 'dst' does not recreate 'src' as valid jsonb |