Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple FULL OUTER JOIN on multiple tables

I have multiple outer joins

SELECT  A.column2         , B.column2         , C.column2 FROM  (     (SELECT month, column2 FROM table1) A     FULL OUTER JOIN     (SELECT month, column2 FROM table2) B on A.month= B.month     FULL OUTER JOIN      (SELECT month, column2 FROM table3) C on A.month= C.month ) 

Now the last join is having a problem, its repeating when month of A is more than B but if B has more month that A we have OUTER JOIN in C with month of A which now repeats, so I guess having a FULL OUTER JOIN within two table might solve the problem?? Any indepth links??

Sample Data(Incorrect)

╔════════════╦═════════╦═════════════╗ ║  Revenue   ║ Budget  ║ ActualMonth ║ ╠════════════╬═════════╬═════════════╣ ║     6.9172 ║ 3.5046  ║ Jan         ║ ║     7.3273 ║ 3.7383  ║ Feb         ║ ║     7.3273 ║ 3.9719  ║ Mar         ║ ║     7.2726 ║ 4.2056  ║ Apr         ║ ║     7.2595 ║ 6.7757  ║ May         ║ ║     7.2726 ║ 6.7757  ║ Jun         ║ ║     0.41   ║ 0.00    ║ Jul         ║ ║     0.41   ║ 0.00    ║ Aug         ║ ║     0.41   ║ 0.00    ║ Sep         ║ ║     0.41   ║ 0.00    ║ Oct         ║ ║     7.4696 ║ 0.00    ║ Nov         ║ ║     7.4696 ║ 0.00    ║ Dec         ║ ║     0.00   ║ 9.3457  ║ Sep         ║ ║     0.00   ║ 16.3551 ║ Dec         ║ ║     0.00   ║ 6.3084  ║ Jul         ║ ║     0.00   ║ 14.0186 ║ Oct         ║ ║     0.00   ║ 16.3551 ║ Nov         ║ ║     0.00   ║ 6.1915  ║ Aug         ║ ╚════════════╩═════════╩═════════════╝ 

Correct Data

╔════════════╦═════════╦═════════════╗ ║  Revenue   ║ Budget  ║ ActualMonth ║ ╠════════════╬═════════╬═════════════╣ ║     6.9172 ║ 3.5046  ║ Jan         ║ ║     7.3273 ║ 3.7383  ║ Feb         ║ ║     7.3273 ║ 3.9719  ║ Mar         ║ ║     7.2726 ║ 4.2056  ║ Apr         ║ ║     7.2595 ║ 6.7757  ║ May         ║ ║     7.2726 ║ 6.7757  ║ Jun         ║ ║     0.41   ║ 6.3084  ║ Jul         ║ ║     0.41   ║ 6.1915  ║ Aug         ║ ║     0.41   ║ 9.3457  ║ Sep         ║ ║     0.41   ║ 14.0186 ║ Oct         ║ ║     7.4696 ║ 16.3551 ║ Nov         ║ ║     7.4696 ║ 16.3551 ║ Dec         ║ ╚════════════╩═════════╩═════════════╝ 
like image 903
Sandip Bantawa Avatar asked Apr 23 '13 11:04

Sandip Bantawa


People also ask

How use full outer join in multiple tables in SQL?

The first, and most fool-proof way, is to use GROUP BY month, and use aggregate functions like MAX(column2) to get the non-zero rows only, or if there are multiple non-zero rows you want to add, use SUM(). This is the best solution if there is an aggregate function that fulfills your logical intent.

Can we join more than 2 tables using join?

Join is a binary operation. More than two tables can be combined using multiple join operations. Understanding the join function is fundamental to understanding relational databases, which are made up of many tables.


1 Answers

SELECT  A.column2         , B.column2         , C.column2 FROM  (     (SELECT month, column2 FROM table1) A     FULL OUTER JOIN     (SELECT month, column2 FROM table2) B on A.month= B.month     FULL OUTER JOIN      (SELECT month, column2 FROM table3) C on ISNULL(A.month, B.month) = C.month ) 
like image 106
Serge Avatar answered Oct 05 '22 23:10

Serge