Solved

TSQL: Specifying days within a year and based on a certain number of years previous

Posted on 2016-07-28
3
31 Views
Last Modified: 2016-07-29
Hello:

The first piece of code below shows credit memos older than five years with an amount greater than 0, while the second piece of code shows credit memos with an amount greater than 0 but between 181 and 365 days from the credit memo's date.

This means that the second piece of code shows data only from the past year.  So, the first piece of code shows credit memos that are older than five years--not just credit memos between 181 and 365 days of this past year.

I need to be able to show the second piece of code at any point in time that I want within the past five years.

So, if I want to see credit memos in the "[181 - 365 Days]" bucket 1 year ago, I want to see that.  If I want to see credit memos from within that same bucket 4 years ago, I want to see that.

Is there a way, through perhaps a common table expression, that I can accomplish this?

Below are screenshots of what data displays when I run the first and second pieces of code, respectively.

Thanks, for the help!  It's much appreciated!

John


--credit memos older than five years whose balance is greater than zero 
select RM20101.CUSTNMBR as [CUSTOMER ID], RM00101.CUSTNAME AS [NAME], SUM(RM20101.CURTRXAM * - 1) AS [AMT REMAINING]
from RM20101 
INNER JOIN RM00101 on RM20101.CUSTNMBR = RM00101.CUSTNMBR
where RMDTYPAL = 7 and DATEDIFF(yyyy, RM20101.DOCDATE, GETDATE()) > 5 and RM20101.CURTRXAM > 0
GROUP BY RM20101.CUSTNMBR, RM00101.CUSTNAME

Open in new window



--credit memos whose amount remains for the [181-365 Days] bucket within this year
SELECT RM20101.CUSTNMBR as [CUSTOMER ID], RM00101.CUSTNAME AS [NAME], SUM(RM20101.CURTRXAM * - 1) AS [AMT REMAINING], 
SUM(CASE WHEN (DATEDIFF(dd, RM20101.DOCDATE, GETDATE()) > 180 
AND DATEDIFF(dd, RM20101.DOCDATE, GETDATE()) <= 365)
THEN RM20101.CURTRXAM * - 1 ELSE 0 END) AS [181-365 DAYS]
FROM RM20101 
INNER JOIN RM00101 ON RM20101.CUSTNMBR = RM00101.CUSTNMBR 
WHERE (RM20101.VOIDSTTS = 0) AND (RM20101.CURTRXAM > 0) AND RM20101.RMDTYPAL = 7
and DATEDIFF(dd, RM20101.DOCDATE, GETDATE()) > 180 
and DATEDIFF(dd, RM20101.DOCDATE, GETDATE()) <= 365
GROUP BY RM20101.CUSTNMBR, RM00101.CUSTNAME

Open in new window



FirstSecond
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 Comments
 
LVL 5

Assisted Solution

by:Brian Chan
Brian Chan earned 250 total points
ID: 41734015
Would you mean this? I just mimic your logic from your second query.....

SELECT 
	RM20101.CUSTNMBR as [CUSTOMER ID]
	, RM00101.CUSTNAME AS [NAME]
	, SUM(RM20101.CURTRXAM * - 1) AS [AMT REMAINING]
	, SUM(	
			CASE WHEN (DATEDIFF(yyyy, RM20101.DOCDATE, GETDATE()) > 5 )
				THEN RM20101.CURTRXAM * - 1
			ELSE 0 END
		) AS [Past 5 years]
FROM RM20101 
	INNER JOIN RM00101 ON RM20101.CUSTNMBR = RM00101.CUSTNMBR
WHERE RMDTYPAL = 7
	AND RM20101.CURTRXAM > 0
	AND (RM20101.VOIDSTTS = 0) 
	AND DATEDIFF(yyyy, RM20101.DOCDATE, GETDATE()) > 5 
GROUP BY RM20101.CUSTNMBR, RM00101.CUSTNAME

Open in new window

0
 
LVL 41

Accepted Solution

by:
Sharath earned 250 total points
ID: 41734188
Multiple your bucket with a 1 or 2 or 3 to go back 1/2/3 years.
DECLARE @year INT
SELECT @year = 1;
--credit memos whose amount remains for the [181-365 Days] bucket within this year
SELECT RM20101.CUSTNMBR as [CUSTOMER ID], RM00101.CUSTNAME AS [NAME], SUM(RM20101.CURTRXAM * - 1) AS [AMT REMAINING], 
SUM(CASE WHEN (DATEDIFF(dd, RM20101.DOCDATE, GETDATE()) > 180 
AND DATEDIFF(dd, RM20101.DOCDATE, GETDATE()) <= 365)
THEN RM20101.CURTRXAM * - 1 ELSE 0 END) AS [181-365 DAYS]
FROM RM20101 
INNER JOIN RM00101 ON RM20101.CUSTNMBR = RM00101.CUSTNMBR 
WHERE (RM20101.VOIDSTTS = 0) AND (RM20101.CURTRXAM > 0) AND RM20101.RMDTYPAL = 7
and DATEDIFF(dd, RM20101.DOCDATE, GETDATE()) > @year*180 
and DATEDIFF(dd, RM20101.DOCDATE, GETDATE()) <= @year*365
GROUP BY RM20101.CUSTNMBR, RM00101.CUSTNAME

Open in new window

0
 

Author Closing Comment

by:John Ellis
ID: 41734819
Thank you, both!
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
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…

752 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