Oracle SQL - Where within a where

Hello Experts!
i'm trying to write a SQL query where , based on a value of a column, the where condition is different.
is it possible?
thanks
eagle_eaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

slightwv (䄆 Netminder) Commented:
Anything might be possible with AND/OR logic but I'm afraid we'll need more information.

Sample data and expected results with an explanation of your requirement would help a lot.
0
PortletPaulfreelancerCommented:
more details are required to answer this
perhaps post your existing query and explain what conditions you want to add.

at the moment the answer is: "probably"

here's an example:
where (
        ( a = 1 and b =2 ) -- "where 1"
      OR
        ( x = 55 and y = 66 ) -- "where 2"
      )

Open in new window

0
eagle_eaAuthor Commented:
sample query:
select d.sid, s.parameter, s.pvalue Standard, p.pvalue Actual,o.pvalue "Override ", o.owner
  from db_parameters     p,
       db_parameters_std s,
       db_param_trig_override o,
       databases         d,
       machines          m,
       clients           c
 where d.Pversion like s.Aversion || '%'
   and p.db_instance = d.instance
   and s.parameter = p.parameter
   and d.mac_instance = m.instance
   and d.client = c.instance
   and o.db_instance = p.db_instance

   and ((getnumber(s.pvalue) is NULL and
       s.pvalue <> p.pvalue) or (getnumber(s.pvalue) is NOT NULL and
       getnumber(p.pvalue) is NOT NULL and
       getnumber(p.pvalue) <
       getnumber(s.pvalue)))
   and d.sid='dacprd2'
 order by d.Pversion, d.sid;

expected result:
the last column ,OWNER is either - ABC or XYZ or ALL
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

eagle_eaAuthor Commented:
if o.owner is ALL then i want a where condition.
if owner is 'XYZ' a different where cndition
0
slightwv (䄆 Netminder) Commented:
Take a look at the example posted by PortletPaul.  It has the logic I believe you need:


select .....
where ... common to both where clauses ...
and
(
     (
         OWNER='ALL' and whereclause1
     )
     or
     (
         whereclause2
     )
)

Note:  in 'whereclause1' and 2 above, there is no actual WHERE keyword.   Just the column logic.
0
PortletPaulfreelancerCommented:
I strongly encourage indentation and without knowing much about the differeing conditions all I can do is re-iterate what slightwv has outlined above by this example only rework of your code:
sample query:
select d.sid, s.parameter, s.pvalue Standard, p.pvalue Actual,o.pvalue "Override ", o.owner
  from db_parameters     p,
       db_parameters_std s,
       db_param_trig_override o,
       databases         d,
       machines          m,
       clients           c
       /* join logic */
 where p.db_instance = d.instance
   and s.parameter = p.parameter
   and d.mac_instance = m.instance
   and d.client = c.instance
   and o.db_instance = p.db_instance
   
       /* filtering logic */
   and (
           (
           OWNER='ALL'
           and d.Pversion like s.Aversion || '%'
           and (
               getnumber(s.pvalue) is NULL
               and s.pvalue <> p.pvalue) or (getnumber(s.pvalue) is NOT NULL
               and getnumber(p.pvalue) is NOT NULL
               and getnumber(p.pvalue) < getnumber(s.pvalue)
               )
           and d.sid='dacprd2'
           )
        OR
           (
           OWNER='ABC'
           and d.Pversion like s.Aversion || '%'
           and (
               getnumber(s.pvalue) is NULL
               and s.pvalue <> p.pvalue) or (getnumber(s.pvalue) is NOT NULL
               and getnumber(p.pvalue) is NOT NULL
               and getnumber(p.pvalue) < getnumber(s.pvalue)
               )
           and d.sid='dacprd2'
           )
        OR
           (
           OWNER='XYZ'
           and d.Pversion like s.Aversion || '%'
           and (
               getnumber(s.pvalue) is NULL
               and s.pvalue <> p.pvalue) or (getnumber(s.pvalue) is NOT NULL
               and getnumber(p.pvalue) is NOT NULL
               and getnumber(p.pvalue) < getnumber(s.pvalue)
               )
           and d.sid='dacprd2'
           )
        )
order by d.Pversion, d.sid;

Open in new window

If were my own query, I would also use ANSI join syntax so there is absolutely no confusion over join logic and filtering logic.

& please note I have not tried to separate out any "common conditions" in this either
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Oracle Database

From novice to tech pro — start learning today.