Solved

T-SQL: More Fun with Syntax

Posted on 2016-08-02
6
39 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
  • 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Complex SQL script 1 31
data lookup in Oracle - need suggestions 55 104
Query Peformance + mulitple query plans 9 48
Unable to save view in SSMS 21 59
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

895 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

14 Experts available now in Live!

Get 1:1 Help Now