Solved

using Case

Posted on 2014-12-09
3
147 Views
Last Modified: 2014-12-09
Does this query look correct? I am trying to use Case in this select query below.  I want to get where Source is either 'billings' or 'billings and terrritory = '1' then it should be 'Resales'
if Source = 'ERP' or 'IMT' and territory = 'Direct' then it should returns as 'Direct'.

 select
 case
         when Source = 'billings' or Source = 'billingsAdj' and Territory = '1' then 'Resales'
         when Source = 'ERP' OR Source = 'IMT' and Territory = 'Direct' then 'Direct'
         end as Sales  
        
from tbl_sales
0
Comment
Question by:VBdotnet2005
[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
3 Comments
 
LVL 66

Accepted Solution

by:
Jim Horn earned 500 total points
ID: 40489326
Looks correct, but just to be safe I'd include parentheses ( ) to set the order of precedence.
For example, in the first when..then, is it (1 or 2) and 3, or 1 or (2 and 3)?
SELECT
  CASE
      WHEN (Source = 'billings' or Source = 'billingsAdj') and Territory = '1' THEN 'Resales'
      WHEN (Source = 'ERP' OR Source = 'IMT') and Territory = 'Direct' THEN 'Direct'
      END as Sales  
FROM tbl_sales 

Open in new window


Also, might not be a bad idea to include an ELSE line to cover all instances where the first two expressions do not evaluate to True.

Also, if it helps I have an image and code-heavy tutorial out there called SQL Server CASE Solutions.
0
 

Author Comment

by:VBdotnet2005
ID: 40489359
Thank you very much Jim.
0
 
LVL 32

Expert Comment

by:bhess1
ID: 40489376
If I am reading your description correctly, the parentheses suggested by Jim Horn above are actually mandatory, as OR is lower in precedence than AND is.

For example, this piece of your current code:
when Source = 'billings' or Source = 'billingsAdj' and Territory = '1' then 'Resales'
parses as:
when Source = 'billings' or (Source = 'billingsAdj' and Territory = '1') then 'Resales'
But it sounds as if your want to do this:
when (Source = 'billings' or Source = 'billingsAdj') and Territory = '1' then 'Resales'

If I misinterpreted your question, well, the correct answer is also above. Personally, I would include the parentheses in the first case solely for readability, so there is no ambiguity in the code.
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

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.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

726 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