Solved

Stuck on an implementation of sum and groupby

Posted on 2010-11-30
7
302 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
[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
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
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…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

739 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