Search code examples
sqlsql-server-2014

Two Table Returning Similar Data


I've tried every join i can think of but cant work it out :(

I have two tables

Table X and Table Y

Both tables contain Item Code and QTY

What i am looking for is something that can help me compare

Ideally i want to see something like this is it possible ?

example data Table X

+-----------+-----+
| Item Code | QTY |
+-----------+-----+
| A         | 1   |
+-----------+-----+
| B         | 2   |
+-----------+-----+
| C         | 3   |
+-----------+-----+

Table Y

+-----------+-----+
| Item Code | QTY |
+-----------+-----+
| A         | 1   |
+-----------+-----+
| D         | 2   |
+-----------+-----+
| E         | 3   |
+-----------+-----+

End result i want to see something like this :

+-----------+-------------+-------------+
| Item Code | TABLE X QTY | Table Y QTY |
+-----------+-------------+-------------+
| A         | 1           | 1           |
+-----------+-------------+-------------+
| B         | 2           | 0           |
+-----------+-------------+-------------+
| C         | 3           | 0           |
+-----------+-------------+-------------+
| D         | 0           | 2           |
+-----------+-------------+-------------+
| E         | 0           | 3           |
+-----------+-------------+-------------+

is this even possible plz help


Solution

  • This looks like a full join

    select coalesce(x.item, y.item) as item, 
           coalesce(x.qty, 0) as x_qty, coalesce(y.qty, 0) as y_qty
    from x full join
         y
         on x.item = y.item