Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

using Case

Posted on 2014-12-09
3
Medium Priority
?
151 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

610 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