Solved

How can I view detalis and sum info in same query

Posted on 2014-02-03
3
193 Views
Last Modified: 2014-02-03
Hello, I have one table:
[ID]      [Group_ID]       [Amount]
1      1      10
2      1      20
3      2      5


I want to make a query that show details, but also sum by Group_ID.

[ID]       [Amount]
1      10
2      20
      30 (Sum group id 1)
3      5
      5 (Sum group id 2)

Can some help me? (I use MS SQL server 2008 R2)
0
Comment
Question by:tselectro
[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 Comments
 
LVL 66

Accepted Solution

by:
Jim Horn earned 300 total points
ID: 39829688
Here's an example from an article I wrote on SQL Server GROUP BY solutions.
Scroll down to #5.

In your case, where the example has a subquery that returns the Max(), you'll need a subquery that returns a Sum().

Something like..
SELECT yt.ID, yt.Group_ID, yt.Amount, yt_total.the_sum
FROM your_table yt
JOIN (
   SELECT Group_ID, Sum(Amount) as the_sum
   FROM your_table
   GROUP BY Group_ID ) yt_total ON yt.Group_ID = yt_total.Group_ID

Open in new window

Guessing for readability it would be better to include the Group_ID in the query results.
0
 
LVL 69

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 200 total points
ID: 39830094
SELECT
    CASE WHEN ID_ = 'Total' THEN '' ELSE ID_ END AS ID, Amount
FROM (
    SELECT CAST(ID AS varchar(10)) AS ID_, Group_ID, Amount
    FROM dbo.tablename
    UNION ALL
    SELECT 'Total', Group_ID, SUM(Amount)
    FROM dbo.tablename
    GROUP BY Group_ID
) AS derived
ORDER BY
    Group_ID, ID_
0
 

Author Closing Comment

by:tselectro
ID: 39830353
Thank you very much!
0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

728 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