pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Rodrigo De León <rdeleonp@gmail.com>
To: pgsql-sql@postgresql.org
Subject: Re: table juxtaposition
Date: 13 May 2007 22:56:23 -0700
Message-ID: <1179122183.931677.63420@o5g2000hsb.googlegroups.com> (raw)
In-Reply-To: <1178077677.003298.217400@n76g2000hsh.googlegroups.com>
References: <1178077677.003298.217400@n76g2000hsh.googlegroups.com>

On May 1, 10:47 pm, workoutlooktest....@gishpuppy.com wrote:
> Hello all,
> I have a table with 2 issues, issue_A and issue_B, a table with action
> items related to the issues,
>
> issue_A AI_A1
> issue_A AI_A2
> issue_A AI_A3
> issue_B AI_B1
> issue_B AI_B2
>
> and network elements that the issues affect:
>
> issue_A NE_A1
> issue_A NE_A2
> issue_B NE_B1
> issue_B NE_B2
> issue_B NE_B3
>
> and I would like a query whose result juxtaposes those 2 tables, as
> follows:
>
> issue_A AI_A1   NE_A1
> issue_A AI_A2   NE_A2
> issue_A AI_A3   NULL
> issue_B AI_B1   NE_B1
> issue_B AI_B2   NE_B2
> issue_B NULL    NE_B3
>
> where the top and bottom NULLs could also be NE_A2, AI_B2
> respectively, I wouldn't mind.
> Any idea how to do that?
> Thanks, Robin

----------------------------------------

t=# select * from ai;
    i    |  ai
---------+-------
 issue_A | AI_A1
 issue_A | AI_A2
 issue_A | AI_A3
 issue_B | AI_B1
 issue_B | AI_B2

t=# select * from ne;
    i    |  ne
---------+-------
 issue_A | NE_A1
 issue_A | NE_A2
 issue_B | NE_B1
 issue_B | NE_B2
 issue_B | NE_B3

t=# SELECT COALESCE(ai.i, ne.i) AS i, ai.ai, ne.ne
t-# FROM ai FULL JOIN ne ON
t-# REPLACE(ai.ai, 'AI', '') = REPLACE(ne.ne, 'NE', '');
    i    |  ai   |  ne
---------+-------+-------
 issue_A | AI_A1 | NE_A1
 issue_A | AI_A2 | NE_A2
 issue_A | AI_A3 |
 issue_B | AI_B1 | NE_B1
 issue_B | AI_B2 | NE_B2
 issue_B |       | NE_B3

----------------------------------------

HINT: You might want to normalize your tables further to avoid
unnecessary string handling.




Message-ID: <1179122183.931677.63420@o5g2000hsb.googlegroups.com>
Permalink:  ../1179122183.931677.63420@o5g2000hsb.googlegroups.com/
Also on:    postgresql.org/message-id/1179122183.931677.63420@o5g2000hsb.googlegroups.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: rdeleonp@gmail.com
  Subject: Re: table juxtaposition
  In-Reply-To: <1179122183.931677.63420@o5g2000hsb.googlegroups.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox