Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8737133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:26:47+00:00 2026-06-13T10:26:47+00:00

Below I have a T-SQL query that brings back amount of purchased items, less

  • 0

Below I have a T-SQL query that brings back amount of purchased items, less discounts and the total – grouped by month/year of purchase. How can I update the Query to return a Grand Total Row
where I would can add up the amounts in the Total Column?
It would be good to be able to add up all the rows but my
main item is I need to be able to get a grand total. Thanks.

    Select DATENAME(month, [OrderDate]) + ' ' + CAST(YEAR(OrderDate) AS CHAR(4)) 
    AS [Month],
    SUM(Amount) AS [Amount],
    SUM(Discount1) AS [Discount 1],
    SUM(Discount2) AS [Discount 2],
    SUM(Amount - Discount1 - Discount2) AS [Total]
    From 
    Orders
    JOIN Customer on orders.cust_ky=customer.cust_ky
    GROUP BY DATENAME(month, [OrderDate]) + ' ' + CAST(YEAR(OrderDate) AS CHAR(4))
    ORDER BY MAX(OrderDate)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T10:26:49+00:00Added an answer on June 13, 2026 at 10:26 am

    Depending on your version of sql-server, you might be able to implement the rollup function (SQL-Server 2005+):

    Select DATENAME(month, [OrderDate]) + ' ' + CAST(YEAR(OrderDate) AS CHAR(4)) AS [Month],
        SUM(Amount) AS [Amount],
        SUM(Discount1) AS [Discount 1],
        SUM(Discount2) AS [Discount 2],
        SUM(Amount - Discount1 - Discount2) AS [Total]
    From Orders
    JOIN Customer 
        on orders.cust_ky=customer.cust_ky
    GROUP BY ROLLUP(DATENAME(month, [OrderDate]) + ' ' + CAST(YEAR(OrderDate) AS CHAR(4)))
    ORDER BY MAX(OrderDate)
    

    Or you can use a UNION ALL similar to this, where the second query gets the total without the GROUP BY:

    Select DATENAME(month, [OrderDate]) + ' ' + CAST(YEAR(OrderDate) AS CHAR(4)) AS [Month],
        SUM(Amount) AS [Amount],
        SUM(Discount1) AS [Discount 1],
        SUM(Discount2) AS [Discount 2],
        SUM(Amount - Discount1 - Discount2) AS [Total]
    From Orders
    JOIN Customer 
        on orders.cust_ky=customer.cust_ky
    GROUP BY DATENAME(month, [OrderDate]) + ' ' + CAST(YEAR(OrderDate) AS CHAR(4))
    union all
    Select 'Total',
        SUM(Amount) AS [Amount],
        SUM(Discount1) AS [Discount 1],
        SUM(Discount2) AS [Discount 2],
        SUM(Amount - Discount1 - Discount2) AS [Total]
    From Orders
    JOIN Customer 
        on orders.cust_ky=customer.cust_ky
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written an SQL Query that brings the result in below format ResourceName
I have an SQL query (below) that essentially takes a student from tbStudents, and
I'm using Sql-Server, and I have the below query which returns all columns that
I need an sql query that will retrieve all items that have both tags,
I have sql query like shown below the sql server says that their is
I have a T-SQL query as below which queries a table holding the search
I have a problem with the SQL statement detailed below. The query returns the
I have a SQL query that I'm currently solving by doing two queries. I
below I have a query that will get the most common user agents for
I have a sql query that shows the year in one column and the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.