Re: ranked subqueries vs distinct question

From: David McNett <nugget(at)macnugget(dot)org>
To: Karsten Hilbert <Karsten(dot)Hilbert(at)gmx(dot)net>, pgsql-general(at)postgresql(dot)org
Subject: Re: ranked subqueries vs distinct question
Date: 2008-05-14 13:43:31
Message-ID: 65212064-037C-4BC5-A7D7-79DCF9D5AF77@macnugget.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message mian wang 2008-05-14 13:48:20 Re: ranked subqueries vs distinct question
Previous Message Karsten Hilbert 2008-05-14 13:15:56 ranked subqueries vs distinct question