Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I have two relations such as relation1 and relation2. relation1 has columns of A,B,C and relation2 has columns of D,E,F.

I want to add A of relation1 with D of relation2 where C = F. For the C values which do not exist in relation2 must appear and F values which do not appear in relation1 also must appear How to do this postgresql?

share|improve this question
up vote 2 down vote accepted

Use a FULL [OUTER] JOIN to include rows from either side without a matching row on the other side:

SELECT COALESCE(r1.a, 0) + COALESCE(r2.d, 0) AS a_d
FROM   relation1      r1
FULL   JOIN relation2 r2 ON r1.c = r2.f

Also use COALESCE() to catch NULL values substituted for missing columns.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.