Table Structure
|id |location|sub-location|
--------------------------
|1| 70 |115|
|2| 70 |NULL|
|3| 70 |NULL|
Problem:
- If location is 70 and sub-location is 115. Query should return id = 1
- If sub-location is not equal to 115 (any other sub location) , query should return ids 2 and 3.
- Sub-location will come as parameter, if it exists return those specific rows otherwise only rows which contains null in sub location
I am using following query
Select id, location, sub-location
From table1 Where location = @location and (sub-location is null or sub-location = @sub-location)
Which will return id =1 if location is 70 and sub-location is 115. For other values of sub-location query will returns none of the rows which should not be the case. In case sub-location passed in query does not exist, query should return all the rows which matches with location whose sub-location is null