I am using Microsoft SQL Server Management Studio and I am trying to do the following: I want to make a calculation that looks for the last 2 months and the "current" month.
The month and year are defined as follows:
SELECT
ID,
dc.Year * 100 + dc.MonthOfYear AS YYYMM,
dc.Year * 100 + dc.MonthOfYear - 1 AS PrevMonth,
COUNT(1) AS Count_sales,
SUM(sales) AS TotalSales
FROM
xx (NOLOCK) dc
GROUP BY
dc.Year * 100 + dc.[Month Of Year]
The problem occurs when I have 202101
because the previous month is taken as 202102
any know how to handle when the year changes as well please?
I have tried to using the following but in SQL Server, the add_months
function doesn't exists:
Hive SQL Integer YYYYMM previous Months
Any suggestions please?