Re: ranked subqueries vs distinct question - Mailing list pgsql-general

From David McNett
Subject Re: ranked subqueries vs distinct question
Date
Msg-id 65212064-037C-4BC5-A7D7-79DCF9D5AF77@macnugget.org
Whole thread Raw
In response to ranked subqueries vs distinct question  (Karsten Hilbert <Karsten.Hilbert@gmx.net>)
Responses Re: ranked subqueries vs distinct question  (Karsten Hilbert <Karsten.Hilbert@gmx.net>)
List pgsql-general
On May 14, 2008, at 8:15 AM, Karsten Hilbert wrote:
> Effectively I want known-zip cities first, then
> fragment-matching cities but without those already in the
> known-zip list.
>
> Can anyone teach me how I need to do this in SQL ?

I think you've made things far more complicated than you need.  How
about an approach something along these lines...

SELECT
    name,zip,
    (SELECT zip = '04317') as zipmatch
FROM
  dem.urb
ORDER BY zipmatch DESC, name;

This will give you a nice resultset incorporating a boolean field ('t'
or 'f') reflecting whether or not the zip code matches.  Sorting DESC
on that "zipmatch" field will put the trues at the top of your result
set.

No unions, no messy intersection problems.  Much faster.  I hope that
applies to your situation.

-Nugget

pgsql-general by date:

Previous
From: "mian wang"
Date:
Subject: Re: ranked subqueries vs distinct question
Next
From: Karsten Hilbert
Date:
Subject: Re: ranked subqueries vs distinct question