SQL Server 9.0.3068
So, I know I cannot put aggregates in a WHERE clause and this is giving me some trouble with what I think should be a simple query. My goal is to display the sum of sales for items by customer for a certain date range. The date range would be the first sale for the item/customer and all sales up to the following 2 months.
I'd love to do something like where s.DocumentDate between MinDocDate and MaxDocDate but I know I can't. I know I can use aggreagates in the HAVING clause but then I'm forced to group by s.DocumentDate which doesn't work as these are individual days and I want to show the sum of sales for the 2 month period.sql Code:
use SalesData; go select s.CustomerName, s.ItemNumber, sum(s.GrossAmount) GrossSales, min(s.DocumentDate) MinDocDate, dateadd(month, 2, min(s.DocumentDate)) MaxDocDate from dbo.SalesByItem s where s.ItemNumber = '12345' group by s.CustomerName, s.ItemNumber order by s.CustomerName
Can somebody point this SQL newbie in the right direction?![]()




Reply With Quote
