Reputation: 1
Let's say I have two tables as follows.
ALL_SPORTS
ID Name Age Sport Result Date Weather
----------------------------------------------------
1 Jake 12 Basketball Won 1/2/13 Sunny
2 Jill 13 Tennis Lost 2/3/13 Sunny
3 Sam 14 Basketball Won 4/5/14 Cloudy
4 Ann 15 Football Won 6/7/18 Cloudy
5 Will 18 Track Lost 11/12/13 Rainy
&
Sports_results
ID Sport Result Date
----------------------------
1 Bball W 1/2/13
2 Tn L 2/3/13
2 Fball L 1/2/14
3 Fball L 4/9/14
3 Fball W 4/9/14
4 Bball L 8/9/18
5 Tk L 11/12/13
8 pngpng W 9/4/15
ALL_SPORTS
has a lot more columns. Sport_Results
is more specialized. They both use different ways to show Sport name (Sports_results
uses shortened name) and result (Won and lost vs W and L).
There is some overlap between the tables but each might contain something not contained in the other table and vice versa.
I need to make a third table called ALL_RESULTS which gets all the results from ALL_RESULTS and Sports_Results checking for duplicate results. It should get the following values from the tables.
ALL_RESULTS (desired results)
ID Name Age Sport Result Date Weather
---------------------------------------------------
(From ALL_SPORTS)
1 Jake 12 Basketball Won 1/2/13 Sunny
2 Jill 13 Tennis Lost 2/3/13 Sunny
3 Sam 14 Basketball Won 4/5/14 Cloudy
4 Ann 15 Football Won 6/7/18 Cloudy
5 Will 18 Track Lost 11/12/13 Rainy
(From Sports_results)
ID Sport Result Date
-----------------------------
3 Fball L 4/9/14
3 Fball W 4/9/14
4 Bball L 8/9/18
8 pngpng W 9/4/15
Currently I'm doing a Union but since the formats of values contained in both tables is different and ALL_SPORTS
contains more columns, Union cannot find duplicates.
Would love some tips! TIA
Upvotes: 0
Views: 49
Reputation: 1270773
Just select the common columns and convert to the same format:
select a.ID, a.Sport, a.Result, a.Date
from all_sports a
union -- on purpose to remove duplicates
select sr.ID, sr.Sport,
(case sr.Result when 'W' then 'Won' when 'L' then 'Lost' end),
sr.Date
from Sports_results sr;
Upvotes: 1