Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Using Case Statement in Where Clause

Purvesh KFeb 22 2011 — edited Feb 22 2011
Hello All,

I wish to conditionally use different columns for retrieving unique row. This will be dependent upon a parameter passed to the Function/Procedure.

The SQL framed looks as below:

+select *+
from Test_Table
where column_1 = <some_value>
and case when p_call_location = 'A' then column_2 like '%ABC%'
when p_call_location = 'B' then column_2 Not Like '%ABC%'
when p_call_location = 'C' then column_3 like '%EFG%'
when p_call_location = 'D' then column_3 like '%IJKL%'
END;

However, I am not sure if this works, as I am getting a Missing Right Paranthesis error. Major hinderance is the depedency on multiple columns, which would need to be referred for particular scenario.

Can somebody please help me with an example or a skeleton of how a query should be formed?

Appreciate an early reply!!!

Comments

user2309906
HI ...you can use connect_by_root to find all the id's IN a hierarchy ...to find the one's NOT IN hirarachy should not be a problem from there ... any sample data ???
BluShadow
user13304081 wrote:
hye all,

my aim is to retrieve all the positions that are not in a hierarchy...please help me ...
i
What is your database version?
How about providing some create table statements and example data (insert statements) for us?
How about showing what output you would expect from that example data?
Frank Kulash
Hi,

Welcome to the forum!

It might be more efficient to do a CONNECT BY query, and find the nodes that are leaves at the top level:
SELECT	*
FROM	table_x
WHERE	CONNECT_BY_ISLEAF	= 1
AND	LEVEL			= 1
START WITH	parent_id	IS NULL
CONNECT BY	parent_id	= PRIOR id
	AND	LEVEL		<= 2	-- For efficiency
;
Whenever you have a problem, post a little sample data (CREATE TABLE and INSERT statements) and the results you want from that data.
It helps to mention which version of Oracle you're using, especially when asking about CONNECT BY queries, where every version since Oracle 7 has had significant changes.
BluShadow
Or something like this...
SQL> ed
Wrote file afiedt.buf

  1  with t as (select null as mgr, 1 as emp_id, 'Chairman' as position from dual union all
  2             select 1, 2, 'Vice Chairman' from dual union all
  3             select 2, 3, 'Company Director' from dual union all
  4             select 3, 4, 'Sales Manager' from dual union all
  5             select 3, 5, 'Technology Manager' from dual union all
  6             select 3, 6, 'HR Manager' from dual union all
  7             select 4, 7, 'Sales Rep' from dual union all
  8             select 4, 8, 'Sales Rep' from dual union all
  9             select 4, 9, 'Sales Rep' from dual union all
 10             select 10, 10, 'Dodgy Contractor' from dual union all
 11             select 5, 11, 'Software Developer' from dual union all
 12             select 5, 12, 'Software Developer' from dual union all
 13             select 5, 13, 'Network Specialist' from dual union all
 14             select 5, 14, 'Communications Implementer' from dual union all
 15             select 6, 15, 'Rectruitment Agent' from dual union all
 16             select 6, 16, 'Job Marketing Agent' from dual union all
 17             select 6, 17, 'Job Marketing Assistant' from dual)
 18  --
 19  -- END OF TEST DATA
 20  --
 21  select emp_id from t
 22  minus
 23  select emp_id
 24  from   t
 25  connect by mgr = prior emp_id
 26* start with mgr is null
SQL> /

    EMP_ID
----------
        10

SQL>
... but it does depend what the data looks like and what exactly you are trying to achieve.
Nimish Garg
TRY THIS
SELECT 
	EMPNO 
FROM 
	SCOTT.EMP 
WHERE 
	EMPNO NOT IN 
	(
		SELECT 
			EMPNO 
		FROM 
			SCOTT.EMP 
		CONNECT BY PRIOR 
			EMPNO=MGR 
		START WITH MGR = 7566
	)
1 - 5
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Mar 22 2011
Added on Feb 22 2011
3 comments
72,933 views