Ad

I Am Unable To Find The Sum Of Counts In Mysql

- 1 answer

I need the output as sum of all values of c (i.e. 52)

i used the following query to count the number of distinct fields in the table:

mysql> select  count(distinct col1) c from table1 group by date;

+----+
| c  |
+----+
| 51 |
|  1 |
+----+

now I need the sum of these count values.

desired output:

+----+
|sum |
+----+
| 52 |
+----+

I am unable to frame the required query.

Ad

Answer

You can use sub select over your count query to get the sum of all counts returned by inner query

select sum(c) `sum` from (
  select count(distinct col1) c 
  from table1
  group by date
) t
Ad
source: stackoverflow.com
Ad