Solved

How do i aggregate sales by months for multiple stores

Posted on 2009-04-09
2
343 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 6

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now