mysql - Join on matching records and create combinations when no match -
i trying create new table using 2 others. want create record combinations of records while merging 'value' column. want column tells me value this.:
a:
day month random1 random2 value1 01 jan aa xx 12 24 mar bb yy 34 13 feb cc zz 7
b:
day month value2 03 jan 16 24 mar 2
i trying join them on day , month, want create row combinations if there no row match.
i want table follows:
c:
day month random1 random2 value type 01 jan aa xx 12 value1 24 mar bb yy 34 value1 13 feb cc zz 7 value1 03 jan aa xx 16 value2 03 jan aa yy 16 value2 03 jan aa zz 16 value2 03 jan bb xx 16 value2 03 jan bb yy 16 value2 03 jan bb zz 16 value2 03 jan cc xx 16 value2 03 jan cc yy 16 value2 03 jan cc zz 16 value2 24 mar aa xx 2 value2 24 mar aa yy 2 value2 24 mar aa zz 2 value2 24 mar bb xx 2 value2 24 mar bb yy 2 value2 24 mar bb zz 2 value2 24 mar cc xx 2 value2 24 mar cc yy 2 value2 24 mar cc zz 2 value2
my sql query unusually complicated , sure there simple way of doing this. (i writing query in r using sqldf()
, looking sql suggestion open alternatives.)
it appears though result set includes set a, plus set combination of unique values of random1, unique values of random2 , set b.
i use this
select day, month, random1, random2, value1, 'value1' type tablea union select tableb.day, tableb.month, r1.random1, r1.random2, tableb.value2, 'value2' tableb join (select distinct random1 tablea) r1 join (select distinct random2 tablea) r2;
Comments
Post a Comment