Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

T-SQL: More Fun with Syntax

Posted on 2016-08-02
6
Medium Priority
?
54 Views
Last Modified: 2016-08-02
Hello:

Below is a snippet from a T-SQL query that I'm trying to build.  Here, I'm simply trying to add the results mathematically of several case statements.

But, as usual, SQL is giving me errors everywhere.

Please help!

Thanks!

John

SUM((CASE WHEN DATEDIFF(dd, RM20101.DOCDATE, @DATE) <= 0
and RM20101.RMDTYPAL > 6
THEN RM20101.CURTRXAM * - 1  
ELSE 0 
END) +
(CASE WHEN DATEDIFF(dd, RM20101.DOCDATE, @DATE) = 1
AND DATEDIFF(dd, RM20101.DOCDATE, @DATE) <= 30
and RM20101.RMDTYPAL > 6
THEN RM20101.CURTRXAM * - 1  
ELSE 0 
END) + 
(CASE WHEN DATEDIFF(dd, RM20101.DOCDATE, @DATE) = 31
AND DATEDIFF(dd, RM20101.DOCDATE, @DATE) <= 60
and RM20101.RMDTYPAL > 6
THEN RM20101.CURTRXAM * - 1  
 ELSE 0 
END) +
(CASE WHEN DATEDIFF(dd, RM20101.DOCDATE, @DATE) = 61
AND DATEDIFF(dd, RM20101.DOCDATE, @DATE) <= 90
and RM20101.RMDTYPAL > 6
THEN RM20101.CURTRXAM * - 1  
 ELSE 0 
END) +
(CASE WHEN DATEDIFF(dd, RM20101.DOCDATE, @DATE) = 91
AND DATEDIFF(dd, RM20101.DOCDATE, @DATE) <= 180
and RM20101.RMDTYPAL > 6
THEN RM20101.CURTRXAM * - 1  
 ELSE 0 
END) +
(CASE WHEN DATEDIFF(dd, RM20101.DOCDATE, @DATE) = 181
AND DATEDIFF(dd, RM20101.DOCDATE, @DATE) <= 364
and RM20101.RMDTYPAL > 6
THEN RM20101.CURTRXAM * - 1  
 ELSE 0 
END) +
(CASE WHEN DATEDIFF(dd, RM20101.DOCDATE, @DATE) >= 365
and RM20101.RMDTYPAL > 6
THEN RM20101.CURTRXAM * - 1  
 ELSE 0 
END)) as [BACK OUT CREDITS]

Open in new window

0
Comment
Question by:John Ellis
[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
  • 2
6 Comments
 
LVL 6

Expert Comment

by:Manju
ID: 41739826
What's the error ?
0
 
LVL 6

Expert Comment

by:Manju
ID: 41739832
Try adding sum before every case statement
0
 
LVL 49

Accepted Solution

by:
PortletPaul earned 2000 total points
ID: 41739835
what is the actual error message?

there isn't a syntax problem with that snippet
but there may be logic problems
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 49

Expert Comment

by:PortletPaul
ID: 41739868
Here is a re-write of your sql snippet
SUM(
      (CASE WHEN RM20101.RMDTYPAL > 6  AND DATEDIFF(dd, RM20101.DOCDATE, @DATE) <= 0                                                    THEN RM20101.CURTRXAM * -1 ELSE 0 END)
    + (CASE WHEN RM20101.RMDTYPAL > 6  AND DATEDIFF(dd, RM20101.DOCDATE, @DATE) = 1     AND DATEDIFF(dd, RM20101.DOCDATE, @DATE) <= 30  THEN RM20101.CURTRXAM * -1 ELSE 0 END) 
    + (CASE WHEN RM20101.RMDTYPAL > 6  AND DATEDIFF(dd, RM20101.DOCDATE, @DATE) = 31    AND DATEDIFF(dd, RM20101.DOCDATE, @DATE) <= 60  THEN RM20101.CURTRXAM * -1 ELSE 0 END) 
    + (CASE WHEN RM20101.RMDTYPAL > 6  AND DATEDIFF(dd, RM20101.DOCDATE, @DATE) = 61    AND DATEDIFF(dd, RM20101.DOCDATE, @DATE) <= 90  THEN RM20101.CURTRXAM * -1 ELSE 0 END) 
    + (CASE WHEN RM20101.RMDTYPAL > 6  AND DATEDIFF(dd, RM20101.DOCDATE, @DATE) = 91    AND DATEDIFF(dd, RM20101.DOCDATE, @DATE) <= 180 THEN RM20101.CURTRXAM * -1 ELSE 0 END) 
    + (CASE WHEN RM20101.RMDTYPAL > 6  AND DATEDIFF(dd, RM20101.DOCDATE, @DATE) = 181   AND DATEDIFF(dd, RM20101.DOCDATE, @DATE) <= 364 THEN RM20101.CURTRXAM * -1 ELSE 0 END) 
    + (CASE WHEN RM20101.RMDTYPAL > 6  AND DATEDIFF(dd, RM20101.DOCDATE, @DATE) >= 365                                                  THEN RM20101.CURTRXAM * -1 ELSE 0 END)
    ) AS [BACK OUT CREDITS]

Open in new window

It does not really seem very important how many days difference there is; anything from less than zero to greater than 365 is treated that exact same way

why not just do this?

SUM(CASE WHEN RM20101.RMDTYPAL > 6 AND RM20101.DOCDATE IS NOT NULL THEN RM20101.CURTRXAM * -1 ELSE 0 END) AS [BACK OUT CREDITS]

Open in new window

1
 

Author Comment

by:John Ellis
ID: 41739872
LOL!  Actually, you're right.  There was no syntax error.  I simply didn't put my "DECLARE" and "SET" statements for my @DATE variable at the beginning before I ran the code.

Sorry, for the bother!

John
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 41739878
no problems, but do look at my re-write please, not sure if your case expressions are doing what you expect
1

Featured Post

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

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

In this article I will describe the Backup & Restore 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 needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

715 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