0

I have two tables

      Table1:                                Table2:
ID:      Type:     Amount:               ID:       Amount:
165      Red         300                 188       425           
167      Red         100                 189       500 
168      Blue        250                 222       129
188      Grey        NULL                333       247 
189      Grey        NULL                369       328 

I am trying to replace the null values from table 1 by joining on table 2.

My code results in two amount columns.

LEFT JOIN table2 
ON table2.pk = table1.pk
AND table1.Type IS NULL

2 Answers 2

2

I think you want:

select t1.id, t1.type, coalesce(t1.amount, t2.amount)
from table1 t1 left join
     table2 t2
     on t1.id = t2.id;
Sign up to request clarification or add additional context in comments.

Comments

0

Hope this helps for your needs

select t1.id,t1.type,isnull(t1.amount,t2.amount) 'Amount' from table1 t1 left join  table2 t2 on t1.id = t2.id

you can use isnull() function so that if the first table amount is null then it will take second table value.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.