You should use a CASE
statement.
Eg:
SELECT
CASE
WHEN Appr_ID IS NULL THEN (
SELECT somevalue
FROM sometable
JOIN ...
JOIN ...
WHERE something=something)
ELSE (SELECT somevalue
FROM someothertable
JOIN ...
JOIN ...
WHERE something=something)
END as Your_Result_ColumnName
FROM
YourTableWithApprIDInIt
This will be quite efficient if each sub-query is basically returning the same result regardless of some value in the main YourTableWithApprIDInIt
table (eg Appr_ID
iself). This is because that sub-query won't actually be executed over and over for each row; it's result would be cached by the DBMS and re-used for each row.
But if the subquery depends on Appr_ID
(such that each subquery needs to be re-executed on each row), then rather than using sub-queries, you would probably use a JOIN
instead (in your main FROM
clause).
SELECT
CASE
WHEN a.Appr_ID IS NULL THEN b.somevalue
ELSE c.somevalue
END as Your_Result_ColumnName
FROM
YourTableWithApprIDInIt a
JOIN sometable b ON a.x = b.x
JOIN ... ON ..
JOIN someothertable c ON a.x = c.x
JOIN ... ON ..
You could still use subqueries in this scenario if you wanted, but it might not be so clean (and will possibly be less efficient, as the DBMS will be handling multiple logically disjointed result sets - although I say "possibly" because the DBMS may optimise this properly).
I'd test under your specific scenario to see if one approach yields better results than the other. If performance is even a factor.
Update:
In the case where you need to select multiple values depending on your Appr_ID
value being null or not, then you'd need to repeat the case
statement for each column in your main query, eg:
SELECT
CASE
WHEN Appr_ID IS NULL THEN (
SELECT somevalue_1
FROM sometable_1
JOIN ...
JOIN ...
WHERE something=something)
ELSE (SELECT somevalue_1
FROM someothertable_1
JOIN ...
JOIN ...
WHERE something=something)
END as Your_Result_ColumnName_1,
CASE
WHEN Appr_ID IS NULL THEN (
SELECT somevalue_2
FROM sometable_2
JOIN ...
JOIN ...
WHERE something=something)
ELSE (SELECT somevalue_2
FROM someothertable_2
JOIN ...
JOIN ...
WHERE something=something)
END as Your_Result_ColumnName_2,
...
FROM
YourTableWithApprIDInIt
Or
SELECT
CASE
WHEN a.Appr_ID IS NULL THEN b.somevalue_1
ELSE c.somevalue_1
END as Your_Result_ColumnName_1,
CASE
WHEN a.Appr_ID IS NULL THEN b.somevalue_2
ELSE c.somevalue_2
END as Your_Result_ColumnName_2,
...
FROM
YourTableWithApprIDInIt a
JOIN sometable b ON a.x = b.x
JOIN ... ON ..
JOIN someothertable c ON a.x = c.x
JOIN ... ON ..
This is when the join syntax is really starting to look cleaner... But again, if each of your Your_Result_ColumnName_X
s are coming from different tables, and each of these subqueries doesn't depend on values from inside your main outer query (such that each only needs to be evaluated only one time), the first of the two patterns may be more efficient even though it doesn't look cleaner ;-)
But again, test both in your specific scenario to see if there's a difference, and it may be that they're evaluated/optimised/executed by the DBMS to be exaclty same (in which case, choose the syntax pattern that makes most sense to you!) :-)