getting the sum of values in sql

Mr. T 0 Reputation points
2023-05-10T17:42:46.5766667+00:00

Hi. I have a table output in SQL

Apples 10

Oranges 20

Pears 50

How do I show the sum of thewse values under the values column?

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,113 questions
{count} votes

1 answer

Sort by: Most helpful
  1. LiHongMSFT-4306 24,926 Reputation points
    2023-05-11T01:33:06.8133333+00:00

    Hi @Mr. T

    Please try this query:

    Declare @temp table(fruit varchar(20), amount int)
    insert into @temp values
    ('Apples',10),
    ('Oranges',20),
    ('Pears',50)
    
    SELECT fruit,SUM(amount) AS Amount
    FROM @temp
    GROUP BY GROUPING SETS(fruit,())
    

    Output:

    User's image

    Best regards,

    Cosmog Hong


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments