MySQL query - using SUM of COUNT

后端 未结 5 931
刺人心
刺人心 2021-01-07 16:27

This query:

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

Returns about 1500 (the number I\'m looking

相关标签:
5条回答
  • 2021-01-07 16:38
    SELECT COUNT(count) FROM (SELECT COUNT(source) AS count
    FROM call_details
    GROUP BY source
    HAVING count > 1) as A
    
    0 讨论(0)
  • 2021-01-07 16:47

    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...
    )
    
    0 讨论(0)
  • 2021-01-07 16:56

    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
    
    0 讨论(0)
  • 2021-01-07 16:57

    Try this

    select mycount, sum(mycount) as sumcount
    from
    (SELECT COUNT(source) AS mycount FROM call_details GROUP BY source HAVING mycount >1)   counttable 
    
    0 讨论(0)
  • 2021-01-07 17:01

    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.

    0 讨论(0)
提交回复
热议问题