Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Querying previous months data

$
0
0

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.


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>