Re: We are not following the spec for HAVING without GROUP BY

Поиск
Список
Период
Сортировка
От Tom Lane
Тема Re: We are not following the spec for HAVING without GROUP BY
Дата
Msg-id 16153.1110476690@sss.pgh.pa.us
обсуждение исходный текст
Ответ на We are not following the spec for HAVING without GROUP BY  (Tom Lane <tgl@sss.pgh.pa.us>)
Ответы Re: We are not following the spec for HAVING without GROUP  (Kevin HaleBoyes <khaleboyes@chartwelltechnology.com>)
Re: We are not following the spec for HAVING without GROUP BY  (johnnnnnn <john@phaedrusdeinus.org>)
Re: We are not following the spec for HAVING without GROUP BY  (Jaime Casanova <systemguards@gmail.com>)
Re: We are not following the spec for HAVING without GROUP  (Mark Kirkwood <markir@coretech.co.nz>)
Re: We are not following the spec for HAVING without GROUP  (Heikki Linnakangas <hlinnaka@iki.fi>)
Re: [BUGS] We are not following the spec for HAVING without GROUP BY  (Mark Shewmaker <mark@primefactor.com>)
Re: [BUGS] We are not following the spec for HAVING without  (Gary Doades <gpd@gpdnet.co.uk>)
Список pgsql-hackers
I wrote:
> This is quite clear that the output of a HAVING clause is a "grouped
> table" no matter whether the query uses GROUP BY or aggregates or not.

> What that means is that neither the HAVING clause nor the targetlist
> can use any ungrouped columns except within aggregate calls; that is,
>     select col from tab having 2>1
> is in fact illegal per SQL spec, because col isn't a grouping column
> (there are no grouping columns in this query).

Actually, it's even more than that: a query with HAVING and no GROUP BY
should always return 1 row (if the HAVING succeeds) or 0 rows (if not).
If there are no aggregates, the entire from/where clause can be thrown
away, because it can have no impact on the result!

Would those of you with access to other DBMSes try this:

create table tab (col integer);
select 1 from tab having 1=0;
select 1 from tab having 1=1;
insert into tab values(1);
insert into tab values(2);
select 1 from tab having 1=0;
select 1 from tab having 1=1;

I claim that a SQL-conformant database will return 0, 1, 0, and 1 rows
from the 4 selects --- that is, the contents of tab make no difference
at all.  (MySQL returns 0, 0, 0, and 2 rows, so they are definitely
copying our mistake...)
        regards, tom lane


В списке pgsql-hackers по дате отправления:

Предыдущее
От: Tom Lane
Дата:
Сообщение: Re: [ADMIN] Too frequent warnings for wraparound failure
Следующее
От: Ioannis Theoharis
Дата:
Сообщение: Raw size