Solved

Stuck on an implementation of sum and groupby

Posted on 2010-11-30
7
301 Views
Last Modified: 2012-06-21
Let say i have the following table i.e. .

ID, CLIENTID, TOTAL
1    1   1
2    1   1
3    2   5
4    3   4
5    3   5

I need the to create a sum of TOTAL to output something like

ClientId Total
1 2
2 5
3 9

Can you please confirm how i do this

thanks
Webbo
0
Comment
Question by:Webbo_1980
  • 3
  • 3
7 Comments
 
LVL 6

Accepted Solution

by:
CaptainGiblets earned 500 total points
ID: 34238870
SELECT clientid, SUM(total) AS Total FROM tablename GROUP BY clientid
0
 

Author Comment

by:Webbo_1980
ID: 34239395
That's the first thing i tried but it doesnt work i.e. get results such as e.g. please see below?

Any suggestions?

770      258
770      10
770      7
770      1709
770      20
770      75
770      18
770      9
793      8
793      14
793      1967
0
 
LVL 6

Expert Comment

by:CaptainGiblets
ID: 34239412
i created a sample table with the information you posted and it works fine, what data types are your columns?
0
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 

Author Comment

by:Webbo_1980
ID: 34239856
Both are integer and my exact query is...

SELECT Id,  sum(Total) as totalSum from mytable
group by Id, Total
order by Id


0
 
LVL 6

Expert Comment

by:CaptainGiblets
ID: 34239909
you dont want to group by total, only ClientID. also, if you select ID it will not work as each record in ID is unique
0
 

Author Comment

by:Webbo_1980
ID: 34243332
But the problem is this wont give me a total count i.e. if you look at my samples data i.e. client id 1 and 3 you'll see the total values get added not the amount of clientids

Hope this makes sense?
0
 
LVL 15

Expert Comment

by:derekkromm
ID: 34245305
You may need to re-word your question. You stated that you're looking for a combination of Client ID and their Totals.

The query provided by CaptainGiblets does exactly that and his followup is correct.

You need to change the Id in your query to ClientId

SELECT ClientId,  sum(Total) as totalSum from mytable
group by ClientId
order by ClientId
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Consolidate rows 3 24
SQL 2014 missing dll from Bin? 3 31
Convert time stamp to date 2 54
Make an array show the subkey and put it in a query 2 20
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…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

679 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