Source Table Structures
I have two tables that I would like to combine. Two columns act as keys, and rest of fields are values or attributes.
TABLE A (Has both KeyA and KeyB) :
KEYA | KEYB | Val1 | Val2 | Val3 | ... | ValN
---------------------------------------------
keyA1| keyB1| V11 | V12 | V13 | ... | V1n
keyA1| keyB2| V21 | V22 | V23 | ... | V2n
keyA1| keyB3| V11 | V12 | V13 | ... | V1n
....
keyAm| keyB1| Vm1 | Vm2 | Vm3 | ... | Vmn
Table B (only keyA present):
KEYA | Str1 | Str2
----------------------
keyA1| str11 | str12
keyA2| str21 | str22
....
keyAm| strm1 | strm2
Result Table Structure Needed
I would like to combine the two tables like so
Str1
and Str2
from TABLEB are to be mapped to TABLEA using KeyA.
TABLE C (contains both KeyA and KeyB) :
KEYA | KEYB | Val1 | Val2 | Val3 | ... | ValN | Str1 | Str2
------------------------------------------------------------------
keyA1| keyB1| V11 | V12 | V13 | ... | V1n | str 11| str 12
keyA1| keyB2| V21 | V22 | V23 | ... | V2n | str 11| str 12
keyA2| keyB3| V11 | V12 | V13 | ... | V1n | str 21| str 22
....
keyAm| keyB1| Vm1 | Vm2 | Vm3 | ... | Vmn | str m1| str m2
Issue
Sometimes, new value fields (Val n) get added to Table A, but schema for Table B remains constant. If I combine the tables like so -
create TABLEC as
select a.*, b.Str1 as Str1, b.Str2 as Str2 from
TABLEA a INNER JOIN TABLEB b ON a.keyA=b.keyA
The resultant table has field names as a.val1
, a.val2
etc.
This is what I currently use
create TABLEC as
select a.Val1 as Val1,
a.Val2 as Val2,
...
a.Valn as Valn,
b.Str1 as Str1,
b.Str2 as Str2
from TABLEA a INNER JOIN TABLEB b ON a.keyA=b.keyA
I have to modify code everytime an additional Val field is added. Is there a way to create TABLEC directly so that the field names from TABLEA and TABLEB are preserved (keyA would be the common field).