Solved

Return all records from two tables

Posted on 2013-06-12
4
364 Views
Last Modified: 2013-08-02
Hi.

I have two tables with fields
date, member, value
In the two tables some records mach on member and I subtract the values but there could be a member i one table but not the other and then I want to get that record as well and the value.

Is this possible?

My current sql is like this (not returning all members)

 SELECT            DATE.a, trade_mbr.a, GA7VAL.a-GA7VAL.b,
 FROM               clearbuy a, clearsell b
 INNER JOIN        trade_mbr.a=trade_mbr.b
 ORDER BY        member.a Asc

Best regards
Oli
0
Comment
Question by:tolvudeild-VS
4 Comments
 
LVL 5

Accepted Solution

by:
DOSLover earned 105 total points
ID: 39242663
I would use a FULL OUTER JOIN to pull records from both, and COALESCE to select a non-null value as follows:
 SELECT COALESCE(a.trade_mbr,b.trade_mbr), a.DATE., b.date, COALESCE(a.GA7VAL,0)- COALESCE(b.GA7VAL) as ValueDiff
   FROM clearbuy a
   FULL OUTER JOIN clearsell b ON a.trade_mbr=b.trade_mbr
 ORDER BY  1 

Open in new window

0
 
LVL 37

Assisted Solution

by:momi_sabag
momi_sabag earned 105 total points
ID: 39242667
you want something like this:

with a as (
SELECT            a.date date1, b.date date2, a.trade_mbr mbr_a, b.trade_mbr mbrb,
   a.GA7VAL vala, b.GA7VAL valb
 FROM               clearbuy a
 full outer JOIN    clearsell b on    a.trade_mbr =b.trade_mbr
 ORDER BY        member.a Asc
)
select date1, mbr_a, vala from a where mbrb is null
union all
select date2, mbr_b, valb from a where mbra is null
union all
select date1, mbr_a, vala - valb from a where mbra is not null and mbrb is not null
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

November 2009 Recently, a question came up in the DB2 forum regarding the date format in DB2 UDB for AS/400.  Apparently in UDB LUW (Linux/Unix/Windows), the date format is a system-wide setting, and is not controlled at the session level.  I'm n…
Recursive SQL in UDB/LUW (it really isn't that hard to do) Recursive SQL is most often used to convert columns to rows or rows to columns.  A previous article described the process of converting rows to columns.  This article will build off of th…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question