Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 306
  • Last Modified:

Stuck on an implementation of sum and groupby

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
Webbo_1980
Asked:
Webbo_1980
  • 3
  • 3
1 Solution
 
CaptainGibletsCommented:
SELECT clientid, SUM(total) AS Total FROM tablename GROUP BY clientid
0
 
Webbo_1980Author Commented:
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
 
CaptainGibletsCommented:
i created a sample table with the information you posted and it works fine, what data types are your columns?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Webbo_1980Author Commented:
Both are integer and my exact query is...

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


0
 
CaptainGibletsCommented:
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
 
Webbo_1980Author Commented:
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
 
derekkrommCommented:
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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now