Nested Conditions In SQL
Query
How to put Nested conditions in sql query?
Need to check for some multipul conditions in sql.
Like if 'this ' and 'this' then 'this'.
or when 'this' and 'this' then 'this'.
I saw when and then but what I have to do is not working
in that I want to check this in simple sql query only (plsql not required).
Solution:
Your SQL could look something like this:
SELECT DISTINCT Crit.msgSeqId,
Crit.messageMaster.messageId,
CaseDt.caseCreatedDate,
Rslt.caseId,
PayInstExt.instructionIdentification,
Usr.firstName,
CaseDt.caseCreator,
CAI.caseStatus,
Rslt.sender,
MsgM.messageName,
Rslt.msgDateTime,
Rslt.actionReq,
Rslt.msgStatus,
Rslt.direction,
Rslt.messageReference,
Rslt.msgSeqId,
Crit.msgStatus,
CASE Rslt.direction
WHEN 'incoming'
THEN
CASE
WHEN Crit.messageMaster.messageId
between 1 and 16 or Crit.messageMaster.messageId in (20,21,23,24,25,27,30,31,33)
THEN 'true'
END
WHEN 'outgoing'
THEN
CASE
WHEN Crit.messageMaster.messageId
in (1,2,3,4,5,7,7,8,10,11,12,13,14,15,16,20,21,21,22,23,24,25,26,27,30,31,32,33)
THEN 'false'
END
END AS checkbox
FROM MessageExchanged Crit,
MessageExchanged Rslt,
CaseDetails CaseDt,
CaseAddlInfo CAI,
MessageMaster MsgM,
UserView Usr,
PaymentInstructionExtract PayInstExt
WHERE Crit.messageMaster.messageId IN (5,11,12,14,6,1,20,21,22,23,24,25,26,27,30,31,32,33)
AND Crit.caseId
= Rslt.caseId
AND Crit.caseId
= CAI.caseId
AND CAI.caseOwnerUuid
= Usr.uuid
AND Rslt.msgSeqId
= CaseDt.msgSeqId
AND Rslt.messageMaster.messageId =
MsgM.messageId
AND Rslt.latest
=1
AND Crit.msgSeqId
= PayInstExt.msgSeqId
AND Crit.msgSeqId
= CAI.msgSeqId
AND CAI.caseStatus
<> 'CLSD'
ORDER BY Rslt.msgDateTime DESC
Several notes:
The messageId field appears to be a number, however you
compared it to strings in your query. That leads to implicit conversions
and is potentially dangerous; always avoid these.
The outgoing list of valid IDs is strange; some nubers
were repeated. I've sorted it and put it into an IN list so that it would
be more apparent.
The WHERE clause limits the list of valid messageId's.
Numbers that do not appear there will never make it into selected rows
(eg. messageId will never equal to 1 in the CASE expression), it is therefore
useless and possibly confusing to list them in the CASE expression conditions.
If you remove them, the lists will be shorter and more manageable.
When direction is 'incoming', the value of checkbox will
be either 'true' or null, when direction is 'outgoing', the value of checkbox
will be either 'false' or null. For other values of direction it will be
always null. Is this the expected behaviour? It seems a bit strange to
me.
If your query is slow, it probably is not because of the
CASE expression. Try replacing the computed value of checkbox field with
a constant or null and re-run the statement. I'd expect the runtime to
be practically equal. The problem could lie in the way tables are joined
in your query, may be a missing index or something.
Have a Oracle Question
Do
you have an Oracle Question?
Oracle Books
Oracle Certification,
Database Administration, SQL, Application, Programming Reference Books
Oracle Application
Oracle
Application Hints and Tips
Oracle Home
Oracle
Database, SQL, Application, Programming Tips
All the site contents are Copyright © www.erpgreat.com
and the content authors. All rights reserved.
All product names are trademarks of their respective
companies.
The site www.erpgreat.com is not affiliated with or endorsed
by any company listed at this site.
Every effort is made to ensure the content integrity.
Information used on this site is at your own risk.
The content on this site may not be reproduced
or redistributed without the express written permission of
www.erpgreat.com or the content authors.
|