Thanks, LiSpeedyG! The following is my code:
DECLARE @DATE DATETIME
SET @DATE = '2016-09-30 00:00:00.000'
select RM20101.CUSTNMBR as [CUSTOMER ID], RM00101.CUSTNAME AS [NAME], RM00101.PYMTRMID AS [PAYMENT TERMS],
CASE RM20101.RMDTYPAL
WHEN 1 then 'Invoice'
WHEN 3 then 'Debit Memo'
WHEN 4 then 'Finance Charge'
WHEN 5 then 'Service Repair'
WHEN 6 then 'Warranty'
WHEN 7 then 'Credit Memo'
WHEN 8 then 'Return'
WHEN 9 then 'Payment'
ELSE 'Other'
END as [DOC TYPE],
RM20101.DOCNUMBR as [DOC NUMBER],
CONVERT(VARCHAR(10),RM20101.DOCDATE,101) as [DOC DATE],
CASE CAST(RM20101.DUEDATE as DATE) WHEN '01/01/1900' then NULL else convert(varchar(10), RM20101.DUEDATE, 101)
END as [DUE DATE],
CASE WHEN RM20101.RMDTYPAL < 7 THEN
RM20101.CURTRXAM ELSE RM20101.CURTRXAM * -1
END
as [CURTRXAM],
CASE WHEN DATEDIFF(dd, RM20101.DOCDATE, @DATE) <= 0
and RM20101.RMDTYPAL > 6
THEN RM20101.CURTRXAM * - 1
WHEN DATEDIFF(dd, RM20101.DUEDATE, @DATE) <= 0
and RM20101.RMDTYPAL < 7
THEN RM20101.CURTRXAM ELSE 0
END AS [CURRENT],
CASE WHEN DATEDIFF(dd, RM20101.DOCDATE, @DATE) = 1
AND DATEDIFF(dd, RM20101.DOCDATE, @DATE) <= 30
and RM20101.RMDTYPAL > 6
THEN RM20101.CURTRXAM * - 1
WHEN DATEDIFF(dd, RM20101.DUEDATE, @DATE) = 1
AND DATEDIFF(dd, RM20101.DUEDATE, @DATE) <= 30
and RM20101.RMDTYPAL < 7
THEN RM20101.CURTRXAM ELSE 0
END AS [1-30 DAYS],
CASE WHEN DATEDIFF(dd, RM20101.DOCDATE, @DATE) = 31
AND DATEDIFF(dd, RM20101.DOCDATE, @DATE) <= 60
and RM20101.RMDTYPAL > 6
THEN RM20101.CURTRXAM * - 1
WHEN DATEDIFF(dd, RM20101.DUEDATE, @DATE) = 31
AND DATEDIFF(dd, RM20101.DUEDATE, @DATE) <= 60
and RM20101.RMDTYPAL < 7
THEN RM20101.CURTRXAM ELSE 0
END AS [31-60 DAYS],
CASE WHEN DATEDIFF(dd, RM20101.DOCDATE, @DATE) = 61
AND DATEDIFF(dd, RM20101.DOCDATE, @DATE) <= 90
and RM20101.RMDTYPAL > 6
THEN RM20101.CURTRXAM * - 1
WHEN DATEDIFF(dd, RM20101.DUEDATE, @DATE) = 61
AND DATEDIFF(dd, RM20101.DUEDATE, @DATE) <= 90
and RM20101.RMDTYPAL < 7
THEN RM20101.CURTRXAM ELSE 0
END AS [61-90 DAYS],
CASE WHEN DATEDIFF(dd, RM20101.DOCDATE, @DATE) = 91
AND DATEDIFF(dd, RM20101.DOCDATE, @DATE) <= 180
and RM20101.RMDTYPAL > 6
THEN RM20101.CURTRXAM * - 1
WHEN DATEDIFF(dd, RM20101.DUEDATE, @DATE) = 91
AND DATEDIFF(dd, RM20101.DUEDATE, @DATE) <= 180
and RM20101.RMDTYPAL < 7
THEN RM20101.CURTRXAM ELSE 0
END AS [91 - 180 DAYS],
CASE WHEN DATEDIFF(dd, RM20101.DOCDATE, @DATE) = 181
AND DATEDIFF(dd, RM20101.DOCDATE, @DATE) <= 364
and RM20101.RMDTYPAL > 6
THEN RM20101.CURTRXAM * - 1
WHEN DATEDIFF(dd, RM20101.DUEDATE, @DATE) = 181
AND DATEDIFF(dd, RM20101.DUEDATE, @DATE) <= 364
and RM20101.RMDTYPAL < 7
THEN RM20101.CURTRXAM ELSE 0
END AS [181-364 DAYS],
CASE WHEN DATEDIFF(dd, RM20101.DOCDATE, @DATE) >= 365
and RM20101.RMDTYPAL > 6
THEN RM20101.CURTRXAM * - 1
WHEN DATEDIFF(dd, RM20101.DUEDATE, @DATE) >= 365
and RM20101.RMDTYPAL < 7
THEN RM20101.CURTRXAM ELSE 0
END AS [365 AND OVER],
CASE WHEN DATEDIFF(dd, RM20101.DOCDATE, @DATE) > 180
and RM20101.RMDTYPAL > 6
THEN RM20101.CURTRXAM * - 1
WHEN DATEDIFF(dd, RM20101.DUEDATE, @DATE) > 180
and RM20101.RMDTYPAL < 7
THEN RM20101.CURTRXAM ELSE 0
END AS [>180],
CASE WHEN (CASE WHEN DATEDIFF(dd, RM20101.DOCDATE, @DATE) > 180
and RM20101.RMDTYPAL > 6
THEN RM20101.CURTRXAM * - 1
WHEN DATEDIFF(dd, RM20101.DUEDATE, @DATE) > 180
and RM20101.RMDTYPAL < 7
THEN RM20101.CURTRXAM ELSE 0
END) > 0 THEN
(CASE WHEN DATEDIFF(dd, RM20101.DOCDATE, @DATE) > 180
and RM20101.RMDTYPAL > 6
THEN RM20101.CURTRXAM * - 1
WHEN DATEDIFF(dd, RM20101.DUEDATE, @DATE) > 180
and RM20101.RMDTYPAL < 7
THEN RM20101.CURTRXAM ELSE 0
END) ELSE 0 END AS [BAD DEBT],
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],
CN00500.CRDTMGR as [CRDTMGR],
RM00101.COMMENT1 AS [COMMENT 1], RM00101.COMMENT2 AS [COMMENT 2]
from RM20101
INNER JOIN RM00101 on RM20101.CUSTNMBR = RM00101.CUSTNMBR
LEFT OUTER JOIN CN00500 ON RM20101.CUSTNMBR = CN00500.CUSTNMBR
WHERE (RM20101.VOIDSTTS = 0) AND (RM20101.CURTRXAM <> 0)
GROUP BY RM20101.CUSTNMBR, RM00101.CUSTNAME, RM00101.PYMTRMID, RM20101.RMDTYPAL, RM20101.DOCNUMBR, RM20101.DOCDATE, RM20101.DUEDATE, RM20101.ORTRXAMT,
RM20101.CURTRXAM, CN00500.CRDTMGR, RM00101.COMMENT1, RM00101.COMMENT2
ORDER BY RM20101.CUSTNMBR