DECLARE @startDate datetime
DECLARE @endDate datetime
SELECT YEAR(DATEADD(MONTH, sn.num, TransactionDate)) AS TransactionYear,
MONTH(DATEADD(MONTH, sn.num, TransactionDate)) AS TransactionMonth,
SUM(Coalesce(BaseDebitAmount, 0)-Coalesce(BaseCreditAmount, 0)) AS GrossMonthlySales
FROM sequentialNumbersTable sn
LEFT OUTER JOIN GeneralJournal ON TransactionDate BETWEEN DATEADD(MONTH, sn.num, @startDate) AND DATEADD(MS, -3, DATEADD(MONTH, sn.num + 1, @startDate))
WHERE sn.tally BETWEEN 0 AND DATEDIFF(MONTH, @startDate, @endDate)
AND ...other conditions as before...
|