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

Need help with a special JOIN

From: Andreas <maps(dot)on(at)gmx(dot)net>
To: "pgsql-sql(at)postgresql(dot)org" <pgsql-sql(at)postgresql(dot)org>
Subject: Need help with a special JOIN
Date: 2012-09-29 16:02:23
Message-ID: 50671B8F.4090504@gmx.net (view raw or flat)
Thread:
Lists: pgsql-sql
Hi,

asume I've got 2 tables

objects ( id int, name text )
attributes ( object_id int, value int )

attributes   has a default entry with object_id = 0 and some other where 
another value should be used.

e.g.
objects
(   1,   'A'   ),
(   2,   'B'   ),
(   3,   'C'   )

attributes
(   0,   42   ),
(   2,   99   )

The result of the join should look like this:

object_id, name, value
1,   'A',   42
2,   'B',   99
3,   'C',   42


I could figure something out with 2 JOINs, UNION and some DISTINCT ON 
but this would make my real query rather chunky.   :(

Is there an elegant way to get this?


Responses

pgsql-sql by date

Next:From: Samuel GendlerDate: 2012-09-29 17:32:09
Subject: Re: Need help with a special JOIN
Previous:From: Matthias NagelDate: 2012-09-29 15:46:32
Subject: Re: Reuse temporary calculation results in an SQL update query [SOLVDED]

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