?
Solved

Where Clause with Case statement needed in my query?

Posted on 2009-05-06
4
Medium Priority
?
369 Views
Last Modified: 2012-05-06
I have to fields - may contain combinations of "AWP" or "ACQ" values - the instructions I were given are:
"If both values are AWP, filter should INCLUDE any row where AWP amount > 0.  If both values are ACQ, filter should INCLUDE any row where ACQ amount > 0.  If one values is AWP and the other is ACQ, filter should INCLUDE any row where AWP amount and ACQ amount > 0.  "

Question is - how to I need to construct my WHERE clause to accomodate?  I assume I need some kind of case statement around all of this
0
Comment
Question by:tbaseflug
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 22

Expert Comment

by:pivar
ID: 24320266
Hi,

How about

WHERE 1 = CASE WHEN field1 = 'AWP' AND field2 = 'AWP' AND AWPamount > 0 THEN 1
                            WHEN field1 = 'ACQ AND field2 = 'ACQ'  AND ACQamount > 0 THEN 1
                            WHEN AWPamount > 0  AND ACQamount > 0 THEN 1
                            ELSE 0
                    END


/peter
0
 
LVL 9

Accepted Solution

by:
Ernariash earned 2000 total points
ID: 24320615
Why could you not use simple where like this?
Can your two fields be values others than 'ACQ' and 'AWP'? then you do not need the " IN ('ACQ', 'AWP')"

select * from _TABLE
WHERE ( field1 = 'AWP' AND field2 = 'AWP' AND AWP_Amount > 0 )
OR ( field1 = 'ACQ' AND field2 = 'ACQ'  AND ACQ_amount > 0 ) 
OR (field1 IN ('ACQ', 'AWP') AND field2 IN ('ACQ', 'AWP') AND  field1 <> field2 AND   AWPamount > 0  AND ACQamount > 0 )

Open in new window

0
 

Author Comment

by:tbaseflug
ID: 24320673
The values may be 'ACQ' or 'AWP' or null
0
 
LVL 41

Expert Comment

by:Sharath
ID: 24320935
Eventhough those are NULLs, it won't be a problem. try a Ernariash suggested, you will get what you want.
0

Featured Post

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

649 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