Search code examples
sqlsql-serversql-server-2000

How to make a inner join with condition...?


SQL Server 2000

Using More than 20 views, all the views are inner join with table1 (id and value1 and value2)

Table1

ID Value1 Value2

001 100 null
001 200 null
001 300 null
001 400 null
001 200 null

or

ID Value1 Value2

001 null 100 
001 null 200 
001 null 300 
001 null 400 
001 null 200 

Table2

ID value1 value2

....
....

some data's

from the table1, either value1 or value2 should always be null. now the problem is if the value1 or value2 column is null, then output is showing null because all the views are inner join with id, value1, value2

I want to make a inner join with condition, if the value1 is not null then inner join with value1 or if the value2 is not null then inner join with value2

Query like this

Select * from table2 inner join with table1 on tabel2.id = table1.id and if table1.value1 is null then table2.value2 = table1.value2 or
else table2.value1 = table1= value1 end if

How to make a query for the above condition

Need SQL Query Help


Solution

  • Would something like the following work?

    SELECT * FROM table2
    INNER JOIN WITH table1 ON table2.id = table1.id
        AND (table1.value1 = table2.value1 OR table1.value1 IS NULL)
        AND (table1.value2 = table2.value2 OR table1.value2 IS NULL)
    

    I think this might prove to be quite slow if you had a large data set however...