Solved

Stuck on an implementation of sum and groupby

Posted on 2010-11-30
7
303 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

Question has a verified solution.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how the fundamental information of how to create a table.

717 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