Re: LIMIT BASED ON PERCENT

From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: Another Trad <anothertrad(at)gmail(dot)com>
Cc: Guillaume Lelarge <guillaume(at)lelarge(dot)info>, pgsql-sql(at)postgresql(dot)org, Lee Hachadoorian <lee(dot)hachadoorian(at)gmail(dot)com>
Subject: Re: LIMIT BASED ON PERCENT
Date: 2009-11-18 19:35:22
Message-ID: 162867790911181135u48cbeee0s18f7dee362557e51@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-sql

2009/11/18 Another Trad <anothertrad(at)gmail(dot)com>:
> But there is any way to do it?

CREATE OR REPLACE twenty()
RETURNS SETOF foo AS $$
DECLARE
rows int;
r record;
BEGIN
rows := (SELECT count(*) FROM foo);
FOR r IN EXECUTE 'SELECT * FROM r ORDER BY some col LIMIT ' || (rows
* 0.2)::int LOOP
RETURN NEXT r;
END LOOP;
RETURN;
END;
$$ LANGUAGE plpgsql;

this code should to work in 8.3

regards
Pavel Stehule

>
> 2009/11/18 Guillaume Lelarge <guillaume(at)lelarge(dot)info>
>>
>> Le mercredi 18 novembre 2009 à 20:24:09, Another Trad a écrit :
>> > No, It doesn't.
>> > In my machine:
>> >
>> > First select
>> > ERROR:  syntax error at end of input
>> > LINE 1: select * from rapadura.cliente limit 20%
>> >                                                 ^
>> > Second one:
>> > ERROR:  argument of LIMIT must not contain subqueries
>> >
>> > Postgres 8.3
>> >
>>
>> It works for Lee because obviously he's working on a 8.4 server. You can
>> use
>> subquery in a LIMIT clause if you have an 8.4 PostgreSQL server. For
>> earlier
>> releases, there's no way to do this in a single query.
>>
>>
>> --
>> Guillaume.
>>  http://www.postgresqlfr.org
>>  http://dalibo.com
>
>

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Pavel Stehule 2009-11-18 19:39:34 Re: LIMIT BASED ON PERCENT
Previous Message Lee Hachadoorian 2009-11-18 19:33:53 Re: LIMIT BASED ON PERCENT