Solved

using Case

Posted on 2014-12-09
3
146 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 65

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

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
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 ?
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.
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…

733 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