pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Michael Fuhr <mike@fuhr.org>
To: Neil Dugan <postgres@butterflystitches.com.au>
Cc: pgsql-sql@postgresql.org
Subject: Re: joining two simular (but not identical tables)
Date: Sun, 4 Sep 2005 07:00:31 -0600
Message-ID: <20050904130031.GA9978@winnie.fuhr.org> (raw)
In-Reply-To: <1125836352.7221.7.camel@localhost.localdomain>
References: <1125836352.7221.7.camel@localhost.localdomain>

On Sun, Sep 04, 2005 at 10:19:12PM +1000, Neil Dugan wrote:
> I have two similar but not identical tables.
> I would like to create a view that combines the contents of both tables
> into a single view, where each record in each table is visible as a
> separate record in the view.

Sounds like you're looking for UNION.

http://www.postgresql.org/docs/8.0/static/queries-union.html
http://www.postgresql.org/docs/8.0/static/typeconv-union-case.html

Does the following example do what you want?

CREATE TABLE a (x integer, y integer);
INSERT INTO a (x, y) VALUES (1, 2);
INSERT INTO a (x, y) VALUES (3, 4);

CREATE TABLE b (x integer, z integer);
INSERT INTO b (x, z) VALUES (5, 6);
INSERT INTO b (x, z) VALUES (7, 8);

CREATE VIEW v AS
SELECT x, y, NULL AS z FROM a
UNION ALL
SELECT x, NULL AS y, z FROM b;

SELECT * FROM v;
 x | y | z 
---+---+---
 1 | 2 |  
 3 | 4 |  
 5 |   | 6
 7 |   | 8
(4 rows)

-- 
Michael Fuhr



view thread (2+ messages)

Message-ID: <20050904130031.GA9978@winnie.fuhr.org>
Permalink:  ../20050904130031.GA9978@winnie.fuhr.org/
Also on:    postgresql.org/message-id/20050904130031.GA9978@winnie.fuhr.org

 · 

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: mike@fuhr.org, postgres@butterflystitches.com.au
  Subject: Re: joining two simular (but not identical tables)
  In-Reply-To: <20050904130031.GA9978@winnie.fuhr.org>

* 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