This solution uses a
full join to obtain all rows from LEAGUE1 as well as all rows from
LEAGUE2. The program uses the COALESCE function on each column so
that, whenever possible, there is a value for each column of a row.
Using the COALESCE function on a list of expressions that is enclosed
in parentheses returns the first nonmissing value that is found.
For each row, the following code returns the AvgScore column from
LEAGUE1 for Average:
coalesce(lastyr.avgscore,prioryr.avgscore) as Average format=8.
If this value of AvgScore is missing, then COALESCE returns the AvgScore
column from LEAGUE2 for Average. If this value of AvgScore is missing,
then COALESCE returns a missing value for Average.
In the case of the Name
column, the COALESCE function returns the value of FullName from LEAGUE1
if it exists. If not, then the value is obtained from LEAGUE2 by
using both the TRIM function and concatenation operators to combine
the first name and last name columns:
trim(prioryr.firstname)||' '||prioryr.lastname
Finally, the table is
ordered by Bowler. The Bowler column is the result of the COALESCE
function.
coalesce(lastyr.bowler,prioryr.amfno)as Bowler
Because the value is obtained from either table, you cannot confidently
order the output by either the value of Bowler in LEAGUE1 or the value
of AMFNo in LEAGUE 2, but only by the value that results from the
COALESCE function.