View previous topic :: View next topic
|
Author |
Message |
prgaj1
New User
Joined: 13 Jul 2007 Posts: 18 Location: Pennsylvania
|
|
|
|
Hi,
I need help to use CASE Statement within my WHERE Clause. I want to change the WHERE Clause based on my condition as following:
SELECT ...
FROM ...
WHERE (condition)
AND (condition)
AND (condition)
AND (
CASE WHEN IN_TYPE_FLAG = 'R'
THEN (REGNR_NA = 0) --- Problem
WHEN IN_TYPE_FLAG = 'S'
THEN (REGNR_NA <> 0) --- Problem
-- ELSE -- when in type flag = 'a'
END
)
In the above case, my WHERE Clause is dependent on one of the fields in a table and an input field. If the input field is 'R' then only records where the REGNR_NA is zeroes must be selected; if the input field is 'S' only records with REGNR_NA not equal zeroes must be selected otherwise if the input flag is 'A' then any records can be selected where REGNR_NA is zeroes or not.
I am getting error on the mentioned line (Problem Line). Since CASE is an expression and does not execute a statement, can anyone help me to get my WHERE Clause working... It is specify in my specs that I must use a CASE statement.
Thanks in advance...
[/b] |
|
Back to top |
|
|
Srihari Gonugunta
Active User
Joined: 14 Sep 2007 Posts: 295 Location: Singapore
|
|
|
|
Hi,
You may try this.
Code: |
AND
((REGNR_NA = CASE WHEN :IN_TYPE_FLAG = 'R' THEN 0 END) OR
(REGNR_NA <> CASE WHEN :IN_TYPE_FLAG = 'S' THEN 0 END) OR
(REGNR_NA = CASE WHEN :IN_TYPE_FLAG = 'A' THEN REGNR_NA END)) |
I think its much simpler to use the following.
A
Code: |
ND (( :IN_TYPE_FLAG = 'R' AND REGNR_NA = 0) OR ( :IN_TYPE_FLAG = 'S' AND REGNR_NA <> 0)) |
|
|
Back to top |
|
|
prgaj1
New User
Joined: 13 Jul 2007 Posts: 18 Location: Pennsylvania
|
|
|
|
Thank you very much - I agree it would have easy doing it as one usually do it but the SA (system analyst) wanted us to use a CASE statement... thanks again |
|
Back to top |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19244 Location: Inside the Matrix
|
|
|
|
Hello,
Quote: |
but the SA (system analyst) wanted us to use a CASE statement... |
Suggest it would be good to know why the SA wanted to use CASE. . . |
|
Back to top |
|
|
GuyC
Senior Member
Joined: 11 Aug 2009 Posts: 1281 Location: Belgium
|
|
|
|
a criteria
REGNA_SZ = xxxxxxxxxxxxxxxxx
with xxxxxxxx being something that can be evaluated/calculated once
is Stage 1 and indexable.
most criteria with ORs are stage 2 and not indexable
So maybe the SA has a reason to ask for this.
a possiblilty is
REGNA_SZ between :hvmin and :hvmax
with (hvmin,hvmax) being (0,0), (1,999999) or (0,999999) depending on :type-flag |
|
Back to top |
|
|
|