• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 356
  • Last Modified:

How do i aggregate sales by months for multiple stores

I have a table that has a bunch of sales orders. The table contains StoreIDs, OrderIDs, Total Payments and OrderDate. I have a query which I have included below that gives me a list of all the stores with the total payments (across all orders of that store) for every store.  Now I want to be able to create a report that displays this information per month. However if I include the OrderDate field and try and group by OrderDate and then Store ID,  I get multiple rows for the same store ID for each month for each of the store IDs because each store may have had multiple orders placed in each month. How can i get a result set that has all the storeIDs appearing only once (for each month) with the total payments made for each store.
SELECT     StoreId, SUM(TotalPayments) AS [Total Payments]
FROM         ac_Orders
GROUP BY StoreId
ORDER BY StoreId
 
I have tried this
SELECT     OrderID, StoreId, SUM(TotalCharges) AS [Total Charges], SUM(TotalPayments) AS [Total Payments]
FROM         ac_Orders
GROUP BY OrderID,StoreId
ORDER BY OrderID, StoreId

Open in new window

0
cranialsurge
Asked:
cranialsurge
1 Solution
 
Thomas WheelerCommented:
SELECT     distinct StoreId, SUM(TotalPayments) AS [Total Payments]
FROM         ac_Orders
GROUP BY StoreId
ORDER BY StoreId
0
 
cranialsurgeAuthor Commented:
Got my solution
SELECT     MONTH(OrderDate) AS [Order Date], StoreId, COUNT(OrderId) AS [Number of Orders], SUM(TotalCharges) AS [Total Charges], SUM(TotalPayments)
                      AS [Total Payments]
FROM         ac_Orders
GROUP BY MONTH(OrderDate), StoreId WITH ROLLUP
ORDER BY [Order Date], StoreId
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now