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 15:36:31
Message-ID: C9A6A727-DCA8-4B73-B54A-94298B014845@macnugget.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On May 14, 2008, at 9:55 AM, Karsten Hilbert wrote:
> On Wed, May 14, 2008 at 09:35:10AM -0500, Decibel! wrote:
>
>> SELECT name, zip, zip='04317' AS zipmatch
>> FROM urb LEFT JOIN streets ON (streets.urb_id = urb.urb_id )
>> ORDER BY zipmatch DESC, name
>> ;
>
> The view dem.v_zip2data (which I erronously left out in my
> first post) does just that - it joins streets to urbs
> thereby providing urbs with zip codes from the streets
> table. It, however, only joins those rows which do have a
> zip code. That leaves out those cities which don't. Which
> makes me want to UNION on the dem.urb table in the initial
> problem.

Doesn't the "LEFT JOIN" in decibel's suggestion account for that?
i.e. -- it isn't limited to just rows which have a zip code.
Rows in the result set with no corresponding row in the streets table
will just be represented with a NULL zip code.

If the view is limited as you describe, don't use is. Do the LEFT
JOIN explicitly for yourself and the query should do exactly what you
wish.

Just for a lark, run that code. I'll bet it works for you.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Robert Treat 2008-05-14 15:39:16 Re: Alias in the HAVING clause
Previous Message A B 2008-05-14 15:23:50 how to return parts of records from a function