[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Need help with totals query, in SQL server

Posted on 2016-08-09
4
Medium Priority
?
31 Views
Last Modified: 2016-08-10
I have a table of customer purchases, with the price paid and tax paid for the item. I have a query which will group the number of purchases based on the Month and year, and it works great. See illustration #1 of the result which works great.

However what I need to do is to also display the total of
the purchases per month, plus the total of the taxes paid on all of the purchases made per month. Also I would like the grand total for each month.
I tried doing this using the SUM command but it did not work. Look at Illustration #2 of the desired output which I need. Can someone show me how
to modify my query to display the result shown in Illustration #2. The Grand totals line would also be nice, however my first priority is
to get the monthly totals, TotalPurchasePrice and TotalTaxes. Can someone please help me out with the query?



Table
ID
CustName
PurchaseDate
PurchasePrice
Tax


select YEAR(PurchaseDate) AS Year, MONTH(PurchaseDate) AS Month, COUNT(*) FROM Customers Group By YEAR(PurchaseDate),MONTH(PurchaseDate)

ID     Year   Month  Purchases
1      2016    7        3       -- 3 customer purchases occurred during the month of July
2      2016    8        5
3      2016    9        10



Illustration #2


ID                  Year   Month  Purchases   TotalPurchasePrice      TotalTaxes
1                     2016    7        3           234.44                 55.00
2                     2016    8        5           888.33                 77.00
3                    2016    9        10          987.44                 45.00
                                   
GrandTotals                       18          2110.21                177.00
0
Comment
Question by:brgdotnet
[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
  • 3
4 Comments
 
LVL 49

Expert Comment

by:PortletPaul
ID: 41749748
SELECT
        YEAR(PurchaseDate)  AS Year
      , MONTH(PurchaseDate) AS Month
      , COUNT(*)            AS Purchases
      , SUM(PurchasePrice)  AS TotalPurchasePrice
      , SUM(Tax)            AS TotalTaxes
FROM Customers
GROUP BY
        YEAR(PurchaseDate)
      , MONTH(PurchaseDate)

Open in new window

0
 
LVL 49

Accepted Solution

by:
PortletPaul earned 2000 total points
ID: 41749766
What are you using to display/present the results of the query? e.g. SSRS? HTML?

It is possible to get "grand totals" using SQL, but it isn't always wise to do it that way.

try group by rollup
0
 
LVL 2

Author Comment

by:brgdotnet
ID: 41749806
Hi Paul, thanks for the help. I am not using any type of reporting tools like HTML or SSRS.
It sounds like the grand totals might be too difficult with SQL. I tried your query and it works great. Thanks so much !!
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 41749956
SELECT
        YEAR(PurchaseDate)  AS Year
      , MONTH(PurchaseDate) AS Month
      , COUNT(*)            AS Purchases
      , SUM(PurchasePrice)  AS TotalPurchasePrice
      , SUM(Tax)            AS TotalTaxes
FROM Customers
GROUP BY ROLLUP (
        YEAR(PurchaseDate)
      , MONTH(PurchaseDate)
   )

Open in new window

if you try this you will see extra rows plus the grand total.  Grand totals are possible but a little more complex to achieve so it is typically done through reporting tools.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

656 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