I am going to use a simple example to illistrate the problems I have run into.
Example:
You want to return a query of all insurance policy numbers and their associated insured if they have an insured. So you at first attempt the following outer join:
Unfortunately, eventhough you have an outer join, you will still drop records because of the criteria on employee_type. The common solution to this that I've seen is:
This works better but still may drop some records if you have a policy that has some employees associated with the policy, but none of them are of type 'INSURED'. This is because it will pass the outer join, and thus fail the is null criteria. I've seen the following ugly work around:
In this simple example, the work-around wasn't too bad, but if you had a query with many outer joins that had additional criteria, this would get messy very quickly. Does anyone know of a cleaner approach? I know in later versions of Oracle, you can get around this with the LEFT JOIN key word, but I don't believe Oracle 8 supports this.
Example:
You want to return a query of all insurance policy numbers and their associated insured if they have an insured. So you at first attempt the following outer join:
Code:
SELECT pm.policy_number, emp.Full_Name
FROM policy_master pm, Employee emp
WHERE pm.policy_number = emp.policy_number (+)
AND emp.employee_type = 'INSURED'
Code:
SELECT pm.policy_number, emp.Full_Name
FROM policy_master pm, Employee emp
WHERE pm.policy_number = emp.policy_number (+)
AND (emp.employee_type = 'INSURED' OR
emp.policy_number is null)
Code:
SELECT pm.policy_number, emp.Full_Name
FROM policy_master pm, Employee emp
WHERE pm.policy_number = emp.policy_number
AND emp.employee_type = 'INSURED'
UNION ALL
SELECT pm.policy_number, '' AS Full_Name
FROM policy_master pm
WHERE NOT EXISTS (
SELECT 1 FROM Employee emp
WHERE emp.employee_type = 'INSURED'
AND emp.policy_number = pm.policy_number)