Re: Complex query question

From: "Albe Laurenz" <laurenz(dot)albe(at)wien(dot)gv(dot)at>
To: "Mike Orr *EXTERN*" <sluggoster(at)gmail(dot)com>, <pgsql-general(at)postgresql(dot)org>
Subject: Re: Complex query question
Date: 2011-09-07 08:39:53
Message-ID: D960CB61B694CF459DCFB4B0128514C206D6D3CF@exadv11.host.magwien.gv.at
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Mike Orr wrote:
> I have a complex query question whose answer I think would help me to
> understand subselects and aggregates better. I have a table with four
> columns of interest:
>
> id (int primary key), loc_title (varchar null), loc_value (float
> null), loc_unit (varchar null)
>
> I want the output columns to be:
> (1) each distinct value of loc_title, sorted
> (2) an id of a record containing that loc_title
> (3) the loc_value for the record in column 2
> (4) the loc_unit for the record in column 2
>
> I don't care as much how the records for columns 2-4 are chosen. It
> could be max(loc_value), min(id), or something else. I just need some
> sample records to test my program against.
>
> Is this something I should be able to do with a single query with a
> subselect, or is it too much for one query? I tried a few ways and
> none of them were syntactically valid.

Sorry to disappoint you, but you won't learn a lot about subselects
and aggregates with that:

SELECT DISTINCT ON (loc_title) loc_title, id, loc_value, loc_unit
FROM mytable
ORDER BY loc_title;

Yours,
Laurenz Albe

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Asia 2011-09-07 10:03:45 Re: SSL certificates issue
Previous Message Jayadevan M 2011-09-07 08:24:52 Re: Complex query question