Solved

Where Clause with Case statement needed in my query?

Posted on 2009-05-06
4
331 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Unable to save view in SSMS 21 61
the term "Features Not Supported in the Next Version of SQL Server" 12 30
Sql query 107 33
SQL - insert empty rows into output results 11 27
In this article I will describe the Copy Database Wizard 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.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

863 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now