boombox2014 boombox2014 - 6 days ago 6
SQL Question

Oracle SQL - Joining tables to include one column to the other (vice versa)

These are my sample tables, columns and records...





Table: tbl1
-----------------------
Columns: ID | DEPT | WK | MANHRS
Records: 01 A 1 8
02 A 2 2

Table: tbl2
--------------------------------
Columns: ID | DEPT | WK | WAGES
Records: 01 A 1 3
02 A 2 5


Scenario:

I want to have a result where two tables are joined and MANHRS and WAGES columns are both together in the result set.




Expected output of the result table:
Columns: ID | DEPT | WK | MANHRS | WAGES
01 A 1 8 3
02 A 2 2 5




I tried UNION but didn't get my expected result. :(


How to do this?

Answer

Assuming that you should join by DEPT and WK:

SELECT t1.*, t2.WAGES  
FROM tbl1 t1, tbl2 t2
where t1.DEPT = t2.DEPT and t1.WK = t2.WK
Comments