Solved

Stuck on an implementation of sum and groupby

Posted on 2010-11-30
7
291 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
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…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

746 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now