I'm new to Oracle
and having knowledge of MS SQL
. I'm trying to get a phone number depending upon the user_id
from Table2
and here is the business logic:
Case1: if a single match is found in
Table1
then get it's respective toll free number fromTable2
Case2: if no match is found in
Table1
then get the default toll free number fromTable2
Case3: if an multiple match is found in
Table1
then for all thoseassigned_care_levels
get theCare
value fromTable2
ordered byasc
ordesc
and select the top row phone number.
I wrote the following query which works fine when I run it individually. However, when I cobine it using the if else statements I'm getting the following error ERROR: ORA-00907: missing right parenthesis
. Here is my code:
if ((select count(distinct care_level) from Table1 where user_id = '100') > 0)
select phone from Table2 where care_level in (select distinct care_level from Table1 where user_id = '100')
and rownum = 1
order by care_level asc
else if((select count(distinct care_level) from Table1 where user_id = '100') = 0)
select phone from Table2 where care_level = 'default'
EDIT: ( AS SIngle SQL)