Solved

T-SQL: More Fun with Syntax

Posted on 2016-08-02
6
35 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
Comment Utility
What's the error ?
0
 
LVL 6

Expert Comment

by:Manju
Comment Utility
Try adding sum before every case statement
0
 
LVL 48

Accepted Solution

by:
PortletPaul earned 500 total points
Comment Utility
what is the actual error message?

there isn't a syntax problem with that snippet
but there may be logic problems
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 48

Expert Comment

by:PortletPaul
Comment Utility
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
Comment Utility
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
Comment Utility
no problems, but do look at my re-write please, not sure if your case expressions are doing what you expect
1

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
using & in TSQL 18 21
Word Template Mail merge with vb.net 4 38
Log Backup 2 11
Job Dependencies in SQL Server Agent: 1 11
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

763 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

10 Experts available now in Live!

Get 1:1 Help Now