Re: Displaying two tables side by side

From: Andreas Haumer <andreas(at)xss(dot)co(dot)at>
To: David Garamond <lists(at)zara(dot)6(dot)isreserved(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Displaying two tables side by side
Date: 2004-08-11 17:37:58
Message-ID: 411A5976.4090502@xss.co.at
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi!

David Garamond wrote:
> How can you display two tables side by side? Example:
>
>> select * from t1;
> a | b
> ---+---
> 2 | 2
> 3 | 5
> 4 | 7
> 9 | 0
>
>> select * from t2;
> c | d
> ---+---
> 4 | 5
> 7 | 3
> 3 | 2
> 1 | 1
> 2 | 0
>
> Intended output:
> a | b | c | d
> ---+---+---+---
> 2 | 2 | 4 | 5
> 3 | 5 | 7 | 3
> 4 | 7 | 3 | 2
> 9 | 0 | 1 | 1
> | | 2 | 0
>
> Each table has no keys (and no OIDs). Order is not important, but each
> row from each table needs to be displayed exactly once.
>
You could try to use PosgreSQL's ctid system column to join on like this:

test=# select *,ctid from t1;
a | b | ctid
- ---+---+-------
2 | 2 | (0,1)
3 | 5 | (0,2)
4 | 7 | (0,3)
9 | 0 | (0,4)

test=# select *,ctid from t2;
c | d | ctid
- ---+---+-------
4 | 5 | (0,1)
7 | 3 | (0,2)
3 | 2 | (0,3)
1 | 1 | (0,4)
2 | 0 | (0,5)

test=# select * from t1 right outer join t2 on (t1.ctid=t2.ctid);
a | b | c | d
- ---+---+---+---
2 | 2 | 4 | 5
3 | 5 | 7 | 3
4 | 7 | 3 | 2
9 | 0 | 1 | 1
| | 2 | 0

Note that this is of course very platform specific. On Oracle
you could use rownum, for example.
I don't have a more portable solution on hand right now.

HTH

- - andreas

- --
Andreas Haumer | mailto:andreas(at)xss(dot)co(dot)at
*x Software + Systeme | http://www.xss.co.at/
Karmarschgasse 51/2/20 | Tel: +43-1-6060114-0
A-1100 Vienna, Austria | Fax: +43-1-6060114-71
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFBGll0xJmyeGcXPhERApQ5AKCrOdLg4i6UpycLUGWxTLIpe68C6QCgk2UP
gcXbeO6VEw95obz1D8GQFQk=
=Ksq6
-----END PGP SIGNATURE-----

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Josh Berkus 2004-08-11 17:58:15 Re: Wierded error in recursive function; debugging ideas?
Previous Message Michalis Kabrianis 2004-08-11 17:04:57 Re: Sum and count weird results