Re: sql

From: Richard Huxton <dev(at)archonet(dot)com>
To: "cristi" <cristi(at)dmhi(dot)ct(dot)ro>, <pgsql-sql(at)postgresql(dot)org>
Subject: Re: sql
Date: 2002-12-09 12:04:59
Message-ID: 200212091204.59431.dev@archonet.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Monday 09 Dec 2002 10:40 am, cristi wrote:
> (I want to receive the records which are a combitation of fields of table_a
> and table_b and that are not in the table_c)

The following is one way. Not necessarily the most efficient, but it should be
clear enough. Basically it builds the product of table_a,table_b then uses a
LEFT JOIN with WHERE to find items that don't match.

You'll want to test it against your real data to see if it's fast enough.

richardh=> SELECT * FROM table_a;
a
---
1
2
3
(3 rows)

richardh=> SELECT * FROM table_b;
b
------------
2002-01-01
2002-02-02
2002-03-03
(3 rows)

richardh=> SELECT * FROM table_c;
ca | cb
----+------------
1 | 2002-01-01
1 | 2002-02-02
2 | 2002-02-02
(3 rows)

richardh=> \d view_ab
View "view_ab"
Column | Type | Modifiers
--------+---------+-----------
a | integer |
b | date |
View definition: SELECT table_a.a, table_b.b FROM table_a, table_b;

richardh=> SELECT a,b FROM view_ab LEFT JOIN table_c ON a=ca AND b=cb WHERE ca
IS NULL or cb IS NULL ORDER BY a,b;
a | b
---+------------
1 | 2002-03-03
2 | 2002-01-01
2 | 2002-03-03
3 | 2002-01-01
3 | 2002-02-02
3 | 2002-03-03
(6 rows)

--
Richard Huxton

In response to

  • sql at 2002-12-09 10:40:55 from cristi

Browse pgsql-sql by date

  From Date Subject
Next Message Héctor Iturre 2002-12-09 14:30:49 ISNULL FUNCTION
Previous Message cristi 2002-12-09 10:40:55 sql