Re: How ad an increasing index to a query result?

From: Alban Hertroys <dalroi(at)solfertje(dot)student(dot)utwente(dot)nl>
To: Josip <josip(dot)2000(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: How ad an increasing index to a query result?
Date: 2009-10-18 10:30:08
Message-ID: AC6C3F0E-EC67-4335-B3CA-982CE72A7773@solfertje.student.utwente.nl
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 14 Oct 2009, at 19:05, Josip wrote:

> Hello,
>
> Could somebody please try to help me with this problem?

> I.e., I want to pick the 5 largest entries from table1 and show them
> alongside a new index column that tells the position of the entry. For
> example:
>
> a | b
> --------
> 82 | 5
> 79 | 4
> 34 | 3
> 12 | 2
> 11 | 1

Short of enumerating those results in your application, the easiest
approach is probably to wrap your query in a join with generate_series
like so:

SELECT a, s.b
FROM (
SELECT a
FROM table1
ORDER BY a DESC LIMIT 5
) AS t1, generate_series(5, 1, -1) AS s(b)

Alban Hertroys

--
Screwing up is the best way to attach something to the ceiling.

!DSPAM:737,4adaee3411688629581426!

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Thom Brown 2009-10-18 10:46:06 Re: How ad an increasing index to a query result?
Previous Message Filip Rembiałkowski 2009-10-18 08:15:48 Re: Delete fails with out of memory