tags:

views:

38

answers:

2

Hi friends i am having problem in joining two tables in oracle my two tables are shown bellow

table1 looks like

 id    Name    Jan  
 7001  Deven   22 
 7002  Clause  55 
 7004  Monish  11 
 7010  Dipesh  55
 7070  Dipika  100

table2 looks like

  id      Name       Feb  
  7001    Deven      12 
  7002    Clause     15 
  7003    Nimesh     20 
  7004    Monish     21 
  7005    Ritesh     22 

i want to combine this two table and want answer like bellow

table2 looks like

  id      Name      Jan   Feb  
  7001    Deven     22    12 
  7002    Clause    55    15 
  7003    Nimesh    -     20 
  7004    Monish    11    21 
  7005    Ritesh    -     22 
  7010    Dipesh    55    -
  7070    Dipika    100   -
+1  A: 

Based on the sample data given in the question, I think you have some records only exist in either table1 or table2. So inner join is not applicable. Please use full outer join as follows,

SELECT NVL(table1.id,table2.id), NVL2(table1.id,table1.name,table2.name), table1.jan, table2.feb 
FROM  table1
FULL OUTER JOIN table2 ON table1.id = table2.id

For more info about SQL Joins, refer Wikipedia article

Sujee
+2  A: 

We join asymmetrical records sets using the OUTER JOIN syntax. A LEFT JOIN will provide the results you want:

SQL> select t72.id
  2         , t72.name
  3         , t34.jan
  4         , t72.feb
  5  from t72
  6       left outer join t34
  7       on ( t72.id = t34.id)
  8  order by t72.id
  9  /

        ID NAME                        JAN        FEB
---------- -------------------- ---------- ----------
      7001 Deven                        22         12
      7002 Clause                       55         15
      7003 Nimesh                                  20
      7004 Monish                       11         21
      7005 Ritesh                                  22

SQL>

edit

I note you have amended the sample data while I was knocking up the demo.

When both tables have values of ID which the other table lacks we can use a FULL JOIN to retrieve values from both sides:

SQL> select nvl(t72.id, t34.id) as id
  2         , nvl(t72.name, t34.name) as name
  3         , t34.jan
  4         , t72.feb
  5  from t72
  6       full outer join t34
  7       on ( t72.id = t34.id)
  8  order by t72.id
  9  /

        ID NAME                        JAN        FEB
---------- -------------------- ---------- ----------
      7001 Deven                        22         12
      7002 Clause                       55         15
      7003 Nimesh                                  20
      7004 Monish                       11         21
      7005 Ritesh                                  22
      7010 Dipesh                       55
      7070 Dipika                      100

7 rows selected.

SQL>
APC
Hai friend thanks for help but what can i do when i am having more then two column of such type as "Jan,Fab,Mar,Apr...,...,Dec" till December then how can i solve such problem?
Deven
@Deven - The ideal solution would be to get yourself a schema design which wasn't Teh Suck! But I know how it is, we have to work with what we given :( There is nothing to stop you using a FULL OUTER JOIN on twelve tables. However the performance will probably suffer if your tables have many rows, as the optimzer is likely to plump for Full Table Scans on all of them.
APC