Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How do i aggregate sales by months for multiple stores

Posted on 2009-04-09
2
Medium Priority
?
353 Views
Last Modified: 2013-11-15
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
Comment
Question by:cranialsurge
2 Comments
 
LVL 7

Expert Comment

by:Thomas Wheeler
ID: 24112379
SELECT     distinct StoreId, SUM(TotalPayments) AS [Total Payments]
FROM         ac_Orders
GROUP BY StoreId
ORDER BY StoreId
0
 

Accepted Solution

by:
cranialsurge earned 0 total points
ID: 24112462
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

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

879 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question