UNION and LIMIT issue

Поиск
Список
Период
Сортировка
Искать

UNION and LIMIT issue

От:
Howard Smith <hsmith@visualmining.com>
Дата:
in pgsql 8.2 using LIMIT with UNION is throwing errors...

table is
-------------------------------

name                       quantity
character varying     integer
-------------------------------
Banana                   10
Cherry                    10
Apple                     5
Persimmon              3
Mango                    2

the query :
     SELECT name,quantity FROM fruit limit 3     UNION              select name,CAST(SUM(quantity) as integer) from (             select Cast('Other' as varchar) as name,quantity from fruit             limit ALL offset 3      ) w   group by name

throws an error on the UNION. Removing the first 'Limit 3' fixes the query,
but i need the limit statement. Anybody seen this behavior?

-- 
Howard Smith
professional services engineer
Visual Mining Inc
15825 Shady Grove Road, Suite 20
Rockville, MD 20850
tel 301.795.2239  fax 301.947.8293

Re: UNION and LIMIT issue

От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
Howard Smith  writes:
>       SELECT name,quantity FROM fruit limit 3
>       UNION
>                select name,CAST(SUM(quantity) as integer) from (
>               select Cast('Other' as varchar) as name,quantity from fruit
>               limit ALL offset 3
>        ) w   group by name

You need parentheses:
(SELECT ... limit 3) UNION ...

ISTM that a LIMIT without an ORDER BY is a pretty bad idea, btw.
		regards, tom lane

Re: UNION and LIMIT issue

От:
ljb <lbayuk@pobox.com>
Дата:
tgl@sss.pgh.pa.us wrote:
>...
> ISTM that a LIMIT without an ORDER BY is a pretty bad idea, btw.

Some databases actually output a warning when you do that, telling you your
result is non-deterministic. Which seems to me to be a little presumptuous...

FAQ