Solved

CASE statement in WHERE clause

Posted on 2006-10-22
5
12,509 Views
Last Modified: 2008-01-09
Can i write the following query:

SELECT a,b,c FROM my_table
WHERE    CASE in_emp_id = 0 AND date_from <> 0 THEN exh_valid_from = in_date_from
              , CASE in_emp_id <> 0 AND in_date_from <> 0 THEN exh_emp_id = in_emp_id
              , CASE in_emp_id <> 0 AND in_date_from <> 0 THEN exh_valid_from = in_date_from AND exh_emp_id = in_emp_id;

in_date_from and in_emp_id are input parameters passed to the procedure which contains this query.
exh_emp_id and exh_valid_from are dates from my_table.
Can someone help me out

Thanks
0
Comment
Question by:aaba646
5 Comments
 
LVL 19

Accepted Solution

by:
actonwang earned 168 total points
ID: 17786188
I think that what you want is like :

SELECT a,b,c FROM my_table
WHERE    (in_emp_id = 0 AND date_from <> 0 and exh_valid_from = in_date_from ) or
              (in_emp_id <> 0 AND in_date_from <> 0 and exh_emp_id = in_emp_id) or
              (CASE in_emp_id <> 0 AND in_date_from <> 0 and exh_valid_from = in_date_from AND exh_emp_id = in_emp_id)
/

right :)
Acton
0
 
LVL 14

Assisted Solution

by:GGuzdziol
GGuzdziol earned 166 total points
ID: 17787295
I think You want

SELECT a,b,c
  FROM my_table
  WHERE (in_emp_id = 0 OR exh_emp_id = in_emp_id)
    AND (in_date_from = 0 OR xh_valid_from = in_date_from);
0
 
LVL 18

Assisted Solution

by:Jinesh Kamdar
Jinesh Kamdar earned 166 total points
ID: 17791548
Your 2nd & 3rd CASE conditions do not make sense since they evaluate the same conditions, but return diff. results : -

, CASE in_emp_id <> 0 AND in_date_from <> 0 THEN exh_emp_id = in_emp_id
, CASE in_emp_id <> 0 AND in_date_from <> 0 THEN exh_valid_from = in_date_from AND exh_emp_id = in_emp_id;

I suppose they shud be as below : -

  CASE in_emp_id = 0 AND in_date_from <> 0 THEN exh_valid_from = in_date_from
, CASE in_emp_id <> 0 AND in_date_from = 0 THEN exh_emp_id = in_emp_id
, CASE in_emp_id <> 0 AND in_date_from <> 0 THEN exh_valid_from = in_date_from AND exh_emp_id = in_emp_id;

If i understand ur requirement correctly, u have a procedure that has 2 IN parameters - in_emp_id, in_from_date
that u want to compare to ur table values.

Wat u need is as below : -

SELECT a, b, c FROM my_table
WHERE DECODE(in_emp_id,     0, exh_emp_id,     in_emp_id)     = exh_emp_id
AND     DECODE(in_date_from, 0, exh_valid_from, in_date_from) = exh_valid_from
0
 
LVL 1

Expert Comment

by:Computer101
ID: 20296184
Forced accept.

Computer101
EE Admin
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
levels for reporting 5 65
Need help with Oracle syntax 4 56
Action link in Union Reports Not Working in OBIEE 11g 1 54
join 2 views with 5 conditions 3 44
This article started out as an Experts-Exchange question, which then grew into a quick tip to go along with an IOUG presentation for the Collaborate confernce and then later grew again into a full blown article with expanded functionality and legacy…
Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now