Re: deciding on one of multiple results returned

From: Wes James <comptekki(at)gmail(dot)com>
To: "pgsql-sql(at)postgresql(dot)org" <pgsql-sql(at)postgresql(dot)org>, David Johnston <polobo(at)yahoo(dot)com>
Cc: Seth Gordon <sethg(at)ropine(dot)com>
Subject: Re: deciding on one of multiple results returned
Date: 2012-12-21 17:28:17
Message-ID: CAFjCMHvaZqVeFES5kwc-=vKp7x0dm-CG0t-1Mmayut1vBMW0tg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

David and Seth Thanks. That helped.

When I have

select distinct on (revf3) f1, f2, f3, revers(f3) as revf3 from table
order by revf3

Is there a way to return just f1, f2, f3 in my results and forget revf3 (so
it doesn't show in results)?

Thanks.

On Fri, Dec 21, 2012 at 9:57 AM, Seth Gordon <sethg(at)ropine(dot)com> wrote:

> If you only want one value per id, then your query should be “SELECT
> DISTINCT ON (id) ...”
>
> If you care about which particular value is returned for each ID, then you
> have to sort the results: e.g., if you want the minimum value per id, your
> query should be “SELECT DISTINCT ON (id) ... ORDER BY value”. The database
> will sort the query results before running them through the DISTINCT filter.
>
>
> On Fri, Dec 21, 2012 at 11:31 AM, Wes James <comptekki(at)gmail(dot)com> wrote:
>
>> If a query returns, say the following results:
>>
>> id value
>> 0 a
>> 0 b
>> 0 c
>> 1 a
>> 1 b
>>
>>
>>
>> How do I just choose a preferred element say value 'a' over any other
>> elements returned, that is the value returned is from a subquery to a
>> larger query?
>>
>> Thanks.
>>
>
>

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Scott Marlowe 2012-12-21 18:22:58 Re: deciding on one of multiple results returned
Previous Message Seth Gordon 2012-12-21 16:57:22 Re: deciding on one of multiple results returned