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
Solved

Where Clause with Case statement needed in my query?

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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Suggested Solutions

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
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.

840 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