Re: joining two simular (but not identical tables) - Mailing list pgsql-sql

From Michael Fuhr
Subject Re: joining two simular (but not identical tables)
Date
Msg-id 20050904130031.GA9978@winnie.fuhr.org
Whole thread Raw
In response to joining two simular (but not identical tables)  (Neil Dugan <postgres@butterflystitches.com.au>)
List pgsql-sql
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 |   | 67 |   | 8
(4 rows)

-- 
Michael Fuhr


pgsql-sql by date:

Previous
From: Neil Dugan
Date:
Subject: joining two simular (but not identical tables)
Next
From: Andreas Joseph Krogh
Date:
Subject: Help with UNION query