Solved

T-SQL: More Fun with Syntax

Posted on 2016-08-02
6
49 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 48

Accepted Solution

by:
PortletPaul earned 500 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 48

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 48

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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Suggested Solutions

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
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…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

751 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