?
Solved

using Case

Posted on 2014-12-09
3
Medium Priority
?
148 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 2000 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:Brendt Hess
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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Suggested Courses

777 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