42

This query:

SELECT COUNT(source) AS count
FROM call_details
GROUP BY source
HAVING count >1

Returns about 1500 (the number I'm looking for) results with only the count field. How could I also return the sum of all count fields? When I try

SELECT COUNT(source) AS count,
SUM(count) as total
FROM call_details
GROUP BY source
HAVING count >1

I get an 'Unknown column 'count' in 'field list' error.

And

SELECT COUNT(source) AS count,
SUM(COUNT(source)) as total
FROM call_details
GROUP BY source
HAVING count >1

gives me an 'Invalid use of group function'

Any ideas? I can do a mysql_num_rows($result) of the first set (to get the info I need) but I really want to do it through MySQL.

5 Answers 5

51
SELECT COUNT(count) FROM (SELECT COUNT(source) AS count
FROM call_details
GROUP BY source
HAVING count > 1) as A
Sign up to request clarification or add additional context in comments.

5 Comments

That query executes successfully but it returns a seemingly random number (4999). The query in that subquery run by itself returns 1320. But when I wrap it in the outer select statement it returns 4999.
Actually this is REALLY close. Instead of SUM(count) I did COUNT(count) and it returned what I was looking for. :D If you edit your answer I'll accept it
I changed, it, but I think this is going to return the number sources which have 2 or more calls, instead of the sum. Is that what you wanted?
Yep, that's exactly what I wanted. The field I was looking for was "Repeat calls"
@Paulpro I have a similar question stackoverflow.com/questions/40433497/…
21

You can't get a global total in a row-context. At the time the the COUNT() completes on any particular row, there's nothing to SUM, because the other rows haven't been calculated yet.

You'd have to run the SUM query first to get your individual stats, then sum manually in your script, or re-run the query with a surrounding SUM clause:

SELECT SUM(count) FROM (
   SELECT original query here...
)

3 Comments

Great solution!
Query 1 ERROR: Every derived table must have its own alias
SELECT SUM(count) FROM (SELECT fhmp.posisi_skrg as 'position_jetty', fhmp.model_id as 'form_hulu_migas_id' FROM realisasi_posisi_kapals as fhmp ORDER BY fhmp.id DESC);
5

Try this

select mycount, sum(mycount) as sumcount
from
(SELECT COUNT(source) AS mycount FROM call_details GROUP BY source HAVING mycount >1)   counttable 

2 Comments

Unknown column 'count' in 'having clause' edit: it looks like you forgot to change count in the having clause to mycount. It ran after that but it's still returning 4999.
Edited, it shoule be mycount. Sorry.
2

Assuming you are going to fetch all the results in the application anyway, I think the most efficient way would be to just sum it up in the application code.

Comments

2

Just simply remove the 'Group by' clause in the select query that counts

# first, get your counts by source
SELECT COUNT(source) AS count
FROM call_details
GROUP BY source
HAVING count >1

# then, get the overall total
SELECT COUNT(source) AS count
FROM call_details
HAVING count >1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.