I am currently working a sql query that reads data from a complex sql view that has 20 columns. I should read the data using 8 where conditions. Among those 8 conditions, one condition is optional. Meaning, if there is no matching record that meets all those 8 conditions, omit that optional where condition and return the result.
Here is the example to explain this further::
TABLE:: EMPLOYEE_DETAILS
EMP_NO EMP_NAME EMP_DEPT_NO PREF_LANGUAGE LOCATION
------ -------- ----------- ------------- ---------
1 Name1 101 ENGLISH USA
2 Name2 102 SPANISH SPAIN
3 Name3 103 ENGLISH USA
4 Name4 102 ITALIAN ITALY
5 Name5 103 ENGLISH USA
6 Name6 103 ITALIAN ITALY
QUERY:
SELECT * FROM EMPLOYEE_DETAILS
WHERE EMP_DEPT_NO = 103 AND LOCATION ='USA' AND PREF_LANGUAGE = 'SPANISH'
In this sample sql query PREF_LANGUAGE condition is optional.
If there are records matching all the three conditions return only those matching records .
As the above table has no record that matches all the three conditions, the query should omit optional condition (ie. PREF_LANGUAGE) and result the records that matches the remaining two required conditions.
Any help is highly appreciated.
Thanks