The nullif() isn't required as coalesce will handle that for you.
Select file1.field1, coalesce(file2.field2,file3.field2) from...
Thanks,
Tommy Holden
-----Original Message-----
From: MIDRANGE-L [mailto:midrange-l-bounces@xxxxxxxxxxxx] On Behalf Of Alan Shore
Sent: Friday, June 30, 2017 2:17 PM
To: Midrange Systems Technical Discussion <midrange-l@xxxxxxxxxxxx>
Subject: RE: Using SQl to obtain a field value from one file or another
Thanks Mike
I am playing with the LEFT JOIN and it looks like its working But I will keep in mind what you said
Alan Shore
E-mail : ASHORE@xxxxxxxx
Phone [O] : (631) 200-5019
Phone [C] : (631) 880-8640
'If you're going through hell, keep going.'
Winston Churchill
-----Original Message-----
From: MIDRANGE-L [mailto:midrange-l-bounces@xxxxxxxxxxxx] On Behalf Of Mike Jones
Sent: Friday, June 30, 2017 3:08 PM
To: midrange-l@xxxxxxxxxxxx
Subject: Re: Using SQl to obtain a field value from one file or another
Hi Alan,
In addition to the LEFT JOIN advice you're getting, which is likely needed in your case, you may need or want to add something resembling this to the
mix:
select coalesce( nullif( FILE1.FIELD1, '' ), FILE2.FIELD1 ) as FIELD1
,coalesce( nullif( FILE1.FIELD2, '' ), FILE2.FIELD2 ) as FIELD2 from ...
Code like that delivers the first non-null, non-blank value found, from left to right.
The same result can be accomplished with a CASE expression, but I tend to prefer the above technique as it is less verbose.
I use code like that a lot in cases where a column is being delivered from more than one source column in the database, and a choice needs to be made to deliver the first non-blank column found. You could have two, or more, non-blank values to choose from, in which case the order in which you code the source columns will determine the results delivered.
Numeric columns would look like this:
select coalesce( nullif( FILE1.FIELD1, 0 ), FILE2.FIELD1 ) as FIELD1
,coalesce( nullif( FILE1.FIELD2, 0 ), FILE2.FIELD2 ) as FIELD2 from ...
Mike
From: MIDRANGE-L [mailto:midrange-l-bounces@xxxxxxxxxxxx] On Behalf Of Rob
Berendt
Sent: Friday, June 30, 2017 2:00 PM
To: Midrange Systems Technical Discussion <midrange-l@xxxxxxxxxxxx>
Subject: Re: Using SQl to obtain a field value from one file or
another
with T1 as (
select f1.field1, f2.field2 as col2
from f1 left inner join f2 on f1.field1 = f2.field1 union all select
f1.field1, f3.field2 as col2 from f1 left inner join f3 on f1.field1 =
f3.field2) select t1.field1, t1.col2 from t1 order by t1.field1
Rob Berendt
--
IBM Certified System Administrator - IBM i 6.1 Group Dekko Dept 1600
Mail
to: 2505 Dekko Drive
Garrett, IN 46738
Ship to: Dock 108
6928N 400E
Kendallville, IN 46755
http://www.dekko.com
--
This is the Midrange Systems Technical Discussion (MIDRANGE-L) mailing list To post a message email: MIDRANGE-L@xxxxxxxxxxxx To subscribe, unsubscribe, or change list options,
visit:
http://lists.midrange.com/mailman/listinfo/midrange-l
or email: MIDRANGE-L-request@xxxxxxxxxxxx Before posting, please take a moment to review the archives at
http://archive.midrange.com/midrange-l.
Please contact support@xxxxxxxxxxxx for any subscription related questions.
Help support midrange.com by shopping at amazon.com with our affiliate link:
http://amzn.to/2dEadiD
--
This is the Midrange Systems Technical Discussion (MIDRANGE-L) mailing list To post a message email: MIDRANGE-L@xxxxxxxxxxxx To subscribe, unsubscribe, or change list options,
visit:
http://lists.midrange.com/mailman/listinfo/midrange-l
or email: MIDRANGE-L-request@xxxxxxxxxxxx Before posting, please take a moment to review the archives at
http://archive.midrange.com/midrange-l.
Please contact support@xxxxxxxxxxxx for any subscription related questions.
Help support midrange.com by shopping at amazon.com with our affiliate link:
http://amzn.to/2dEadiD
As an Amazon Associate we earn from qualifying purchases.