Solved

How do i aggregate sales by months for multiple stores

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
optimize stored procedure 6 27
SQL Syntax: How to force case sensitive query? 2 30
access query to sql server 3 20
Proper Case SQL Command 2 9
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

778 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