-1

I have two tables that I am joining

|id |profit|
| 1 | 1234 |
| 2 | 1345 |
| 4 | 1454 |
| 5 | 1254 |

and another table

| id | x loss | y loss |
| 2  | 34312  | 4354   |
| 3  | 35614  | 4365   |
| 4  | 36615  | 4321   |

and combining the two, this would be the result I want

| id | profit | x loss | y loss |
| 1  | 1234   | Null   | Null   |
| 2  | 1345   | 34312  | 4354   |
| 3  | Null   | 35614  | 4365   |
| 4  | 1454   | 36615  | 4321   |
| 5  | 1254   | Null   | Null   |

The code that I used was

Select x.id, y.id, y.profit, x.xloss, x.yloss
from 
    (select id, SUM(xloss) as xloss, SUM(yloss) AS yloss
     from #SYSLOSSINC
     group by id) x 
  full outer join
    (Select id,sum(profit) as profit
     from #MTDPROFIT
     group by ProductCode) y 
  on x.id = y.id

I could not select just the id because of the error: "ambiguous column name 'id'", and this is creating two columns with id as such

| id   | id   | Profit | xloss | yloss|
| Null | 1    | 1234   | Null  | Null |
| 2    | 2    | 1345   | 34312 | 4354 |
| 3    | Null | Null   | 35614 | 4365 |
| 4    | 4    | 1454   | 36615 | 4321 |
| Null | 5    | 1254   | Null  | Null |

How can I combine the id into one column so that null does not show up?

5

This is actually simpler than you think. To get only one ID column, try:

SELECT ISNULL(x.Id, y.Id) as Id,...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.