I have been tasked with pulling the previous two months of data from a table. The view looks like this.
SELECT TOP (100) PERCENT ItemNumber, SUM(ABS(CAST(ROUND(TransactionQty, 2, 1) AS DECIMAL(9, 0)))) AS P2MQS
FROM dbo.IM5_TransactionDetail
WHERE (TransactionCode = N'SO') AND (TransactionRefNumber LIKE N'%-IN') AND (Year = DATEPART(Year, GETDATE())) AND (Period = DATEPART(month, GETDATE()) - 1 OR
Period = DATEPART(month, GETDATE()) - 2)
GROUP BY ItemNumber
ORDER BY ItemNumber
This works great until you hit January or February of the new year. Then of course it returns nothing. In this table there is a field called year and a field called period. Year is the year of the transaction, period is the month of the transaction. Any ideas would be greatly appreciated.