mizan3008
mizan3008

Reputation: 379

How do I sum column after count in mysql query

I used below query

SELECT COUNT(client_mac) AS total_users 
FROM `social_user` 
WHERE social_network ='TWuser' 
GROUP BY client_mac HAVING total_users > 1

after run this query I got this result

Please click here to see my result

But I need to SUM my 'total_users' column so my output should be

total_users: **49**

Sorry for my English and I don't have enough reputation to attach image that's why I added link

Upvotes: 1

Views: 65

Answers (4)

Param Kumar
Param Kumar

Reputation: 123

Try This query -

SELECT SUM(total_users) as total_usr_sum from (SELECT COUNT(client_mac) AS total_users FROM `social_user` WHERE social_network ='TWuser' GROUP BY client_mac HAVING total_users > 1) as sum

Upvotes: 0

Devon Bessemer
Devon Bessemer

Reputation: 35337

I think I finally understand what you're after.

SELECT social_network, count(*) as total_users
FROM social_user
INNER JOIN  (
  SELECT client_mac FROM social_user
  GROUP BY client_mac HAVING count(*) > 1
) as src ON src.client_mac = social_user.client_mac
GROUP BY social_network

So the subquery should get all the client_macs that are repeated then the query will organize each entry by social network.

The problem I foresee is if a client_mac exists for more than one social network.

Upvotes: 1

Mobasher Fasihy
Mobasher Fasihy

Reputation: 1061

I think this may help you

select count(client_mac) as total_user,client_mac 
from social_user
where social_network = 'TWuser'
GROUP BY client_mac`

This query will bring total for each user not all user. fiddle is here

Upvotes: 1

Priyank
Priyank

Reputation: 1009

Just replace COUNT(client_mac) with SUM(total_users) in your query

Upvotes: 0

Related Questions