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
Solved

How can I view detalis and sum info in same query

Posted on 2014-02-03
3
191 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
3 Comments
 
LVL 65

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
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 extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

829 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