Solved

Where Clause with Case statement needed in my query?

Posted on 2009-05-06
4
338 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
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 500 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 40

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

832 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