Solved

Stuck on an implementation of sum and groupby

Posted on 2010-11-30
7
296 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
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.

911 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

21 Experts available now in Live!

Get 1:1 Help Now