Help creating Query

Database

Help creating Query

I need some help with creating a SQL query.

I have 3 tables Table1 (ID1, ID2, dataValue1) , Table2 (ID1, ID2, dataValue2), Table3 (ID1, ID2, datavalue3)
there is zero to many relationship between Table1 and Table2
and zero to many relationship between Table1 and Table3

I want to write a query to get dataValue2 from Table 2 but if there is no matching row between Table 1 and Table 2 then I need to get dataValue3 from Table3. If the corresponding value does not exist in either Table2 and Table3 put 'unknown'

Thanks in advance for your help.
Tags (1)
2 REPLIES
Senior Apprentice

Re: Help creating Query


select
coalesce(t2.datavalue2, t3.datavalue3, 'unknown')
from table1 as t1 left join table2 as t2 on ...
left join table3 as t3 ...

Dieter

Re: Help creating Query

Thanks!! :)