Skip site navigation (1) Skip section navigation (2)

Re: Join required??

From: Vijay Deval <deval(at)giaspn01(dot)vsnl(dot)net(dot)in>
To: Timo Tuomi <tt(at)sllpilots(dot)fi>
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: Join required??
Date: 2001-03-22 12:19:35
Message-ID: 3AB9EDD6.BC923539@giaspn01.vsnl.net.in (view raw or flat)
Thread:
Lists: pgsql-novice
Dear Timo

At present I am using 7.0.2 and eagerly awaiting 7.1. Following qurey gives the
answer you want. It also covers case where table test2 has some rows that have
"i" not in table test1.  I am sure this will work on 6.5 too.


SELECT test1.c as c, (test1.i+test2.i) as i
from test1,test2
WHERE test1.c=test2.c
UNION
SELECT test1.c as name, test1.i as i
from test1
WHERE (not exists (select * from test2 where test1.c=test2.c) )
UNION
SELECT test2.c as name, test2.i as i
from test1
WHERE (not exists (select * from test1 where test1.c=test2.c));

Timo Tuomi wrote:

> testdb=>
> testdb=> select * from test1;
>
> c|i
> -+-
> A|2
> B|3
> C|4
> (3 rows)
>
> testdb=> select * from test2;
>
> c|i
> -+-
> A|6
> C|7
> (2 rows)
>
> testdb=>
>
> I'd like to get (test1.i + test2.i) like this:
>
> c|sum
> -+---
> A|8
> B|3
> C|11
>
> i.e. to sum the rows but if one of the rows doesn't exist on one of the tables
> then print just the existing row. How can I do this with postgresql-6.5.3?
>
> Thanks in advance,
> Timo T.
>
> ---------------------------(end of broadcast)---------------------------
> TIP 6: Have you searched our list archives?
>
> http://www.postgresql.org/search.mpl


In response to

pgsql-novice by date

Next:From: Frank HilliardDate: 2001-03-22 12:51:44
Subject: Creating tables with Cold Fusion
Previous:From: Tom LaneDate: 2001-03-22 06:04:08
Subject: Re: Authentication against /etc/passwd?

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group