I am doing analysis on the Stack Overflow dump.
Problem statement: I have 4 tables and require result in the format given.
Table 1: UserID Year QuestionsOnTopicA
Table 2: UserID Year AnswersOnTopicA
Table 3: UserID Year QuestionsOnTopicB
Table 4: UserID Year AnswersOnTopicB
Desired Output:
UserID Year QuestionsOnTopicA AnswersOnTopicA QuestionsOnTopicB AnswersOnTopicB
UserID column should have entries from all the 4 tables.
I tried performing inner and outer join on the tables but the results were incorrect. Inner join (returns userid present only in first table 1) Outer join (returns other columns only for userid in table 1)
Not sure if union will make sense in this scenario.
Queries are being executed on data.stackexchange.com/stackoverflow
Example
Table 1: 1001, 2010, 5 || 1001, 2011, 3 || 1002, 2010, 4
Table 2: 1001, 2010, 10 || 1001, 2011, 7 || 1002, 2010, 5
Table 3: 1002, 2010, 5
Table 4: 1001, 2010, 10 || 1004, 2011, 5
Output:
1001, 2010, 5 , 10, 0, 10
1001, 2011, 3, 7, 0, 0
1002, 2010, 4, 5, 5, 0
1004, 2011, 0, 0, 0, 5
Ok, this works as intended:
Here is a sqlfiddle with a demo of this.
And the results are: