Skip site navigation (1) Skip section navigation (2)

Re: ORDER BY problem

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Michael Rowan <mike(dot)rowan(at)internode(dot)on(dot)net>
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: ORDER BY problem
Date: 2009-08-15 01:09:26
Message-ID: (view raw, whole thread or download thread mbox)
Lists: pgsql-novice
Michael Rowan <mike(dot)rowan(at)internode(dot)on(dot)net> writes:
> This query selects the most recent advertisement (in table  
> wha_listing) for all properties (in table wha_property) and returns  
> the sales agency (in table wha_agency), as well as other data.  The  
> query works as expected.

> SELECT DISTINCT ON (property_id)
> property_id, date, wha_property.propnum, wha_street.street_name,  
> wha_suburb.suburb_name, wha_agent.first_name, wha_agent.last_name,  
> FROM wha_listing, wha_agent, wha_agency, wha_property, wha_street,  
> wha_suburb
> WHERE = wha_street.suburb_id
> AND = wha_property.street_id
> AND = property_id
> AND = wha_agent.agency_id
> AND = agent_id
> AND >= "2009-01-01" ORDER BY wha_listing.property_id,  

> I need the resulting list to be sorted by

> Can I do this by adding something to this query?

You can't mess with that ORDER BY without messing up the associated
DISTINCT ON behavior.  The trick is to put it all in a subselect:

	  ( ... query as above ... ) ss
	ORDER BY name;

			regards, tom lane

In response to

pgsql-novice by date

Next:From: Jignesh ShahDate: 2009-08-18 10:18:36
Subject: The efficient method to create trigger
Previous:From: Michael RowanDate: 2009-08-15 00:59:43
Subject: ORDER BY problem

Privacy Policy | About PostgreSQL
Copyright © 1996-2017 The PostgreSQL Global Development Group