Add Count () in mysql

1

I have this query that returns me the number of dates equal:

SELECT 'Data', COUNT('Data') AS 'Quantidade' FROM sua_tabela GROUP BY 'Data';
| Data         |
| 2016-06-09   |
| 2016-06-09   |
| 2016-06-09   |
| 2016-06-08   |
| 2016-06-08   |
| 2016-06-05   |
| 2016-06-01   |

Return like this:

| Data       | Quantidade |
| 2016-06-09 |     3      |
| 2016-06-08 |     2      |
| 2016-06-05 |     1      |
| 2016-06-01 |     1      |

I would like to add the Quantity returned for this month

I tried this way:

SELECT Data_reg, SUM(COUNT(Data_reg)) AS 'Soma_total'
FROM Monit 
WHERE Data_reg like ('2016-06%')
GROUP BY 'Data_reg';

However, you returned the following error:

  

SQL Error (1111): Invalid use of group function

    
asked by anonymous 09.02.2018 / 13:12

1 answer

2

You do not need to add COUNT . Just make a COUNT normal that will return the total records LIKE :

SELECT Data_reg, COUNT(Data_reg) AS Soma_total FROM Monit WHERE Data_reg like '2016-06%'

In your case, the above query will return 7 , which is exactly the sum of the values in "Quantity" of your initial query (3 + 2 + 1 + 1 = 7):

| Data       | Quantidade |
| 2016-06-09 |     3      |
| 2016-06-08 |     2      |
| 2016-06-05 |     1      |
| 2016-06-01 |     1      |
    
09.02.2018 / 14:16