Re: Quota query with decent performance?

From: Troels Arvin <troels(at)arvin(dot)dk>
To: pgsql-sql(at)postgresql(dot)org
Subject: Re: Quota query with decent performance?
Date: 2003-11-17 22:55:53
Message-ID: pan.2003.11.17.22.55.52.997736@arvin.dk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Tue, 11 Nov 2003 18:49:31 -0500, Chester Kustarz wrote:

[... discussion of top-n query (where n=3) ...]

> select *
> from person
> where age <=
> (select age from person order by age limit 1 offset 2);

It fails when the cardinality of person is less than 3 (returns empty
set). My solution is this, which is not as beautiful any more:

SELECT *
FROM person
WHERE age <= COALESCE (
(
SELECT age FROM person
ORDER BY age ASC
LIMIT 1 OFFSET 2 -- 2=n-1
),(
SELECT age FROM person
ORDER BY age DESC -- note: opposite of ASC
LIMIT 1
)
);

--
Greetings from Troels Arvin, Copenhagen, Denmark

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Gaetano Mendola 2003-11-17 23:46:50 Re: FATAL 2: PageIndexTupleDelete
Previous Message Andrew Milne 2003-11-17 18:08:40 Function ROWTYPE Parameter with NEW/OLD