Solved

How can I view detalis and sum info in same query

Posted on 2014-02-03
3
190 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SSRS 2013 - Overlapping reports 2 21
SQL SELECT query help 7 37
Query Help - MSSQL - Averages 5 27
T-SQL:  Collapsing 9 23
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

770 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