Solved

How do i aggregate sales by months for multiple stores

Posted on 2009-04-09
2
347 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

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…
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to shrink a transaction log file down to a reasonable size.

756 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