Solved

using Case

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to shrink a transaction log file down to a reasonable size.

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now