UPDATE:
my orgional attempt to use FULL OUTER JOIN did not work correctly. I have updated the question to reflex the true issue. Sorry for presenting a classic XY PROBLEM.
I'm trying to retrieve a dataset from multiple tables all in one query thats is grouped by year, month of the data.
The final result should look like this:
| Year | Month | Col1 | Col2 | Col3 | |------+-------+------+------+------| | 2012 | 11 | 231 | - | - | | 2012 | 12 | 534 | 12 | 13 | | 2013 | 1 | - | 22 | 14 |
Coming from data that looks like this:
Table 1:
| Year | Month | Data | |------+-------+------| | 2012 | 11 | 231 | | 2012 | 12 | 534 |
Table 2:
| Year | Month | Data | |------+-------+------| | 2012 | 12 | 12 | | 2013 | 1 | 22 |
Table 3:
| Year | Month | Data | |------+-------+------| | 2012 | 12 | 13 | | 2013 | 1 | 14 |
I tried using FULL OUTER JOIN
but this doesn't quite work because in my SELECT
clause because no matter which table I select 'Year' and 'Month' from there are null values.
SELECT
Collase(t1.year,t2.year,t3.year)
,Collese(t1.month,t2.month,t3.month)
,t1.data as col1
,t2.data as col2
,t3.data as col3
From t1
FULL OUTER JOIN t2
on t1.year = t2.year and t1.month = t2.month
FULL OUTER JOIN t3
on t1.year = t3.year and t1.month = t3.month
Result is something like this (is too confusing to repeat exactly what i would get using this demo data):
| Year | Month | Col1 | Col2 | Col3 | |------+-------+------+------+------| | 2012 | 11 | 231 | - | - | | 2012 | 12 | 534 | 12 | 13 | | 2013 | 1 | - | 22 | | | - | 1 | - | - | 14 |