Apologies in advance if this question is badly worded!
I have a MySQL table which has a datetime field. How would one count the number of occurrences of each day name, for distinct weeks?
Here is the query I’m trying, and the result it gives from just over 2 weeks of data. It is (obviously) counting every occurrence for each record, whereas I want the output to be either 2 or 3.
SELECT dayname(datetime), COUNT(dayofweek(datetime))
FROM mytable GROUP BY dayofweek(datetime);
+-------------------+----------------------------+
| dayname(datetime) | count(dayofweek(datetime)) |
+-------------------+----------------------------+
| Monday | 404 |
| Tuesday | 275 |
| Wednesday | 251 |
| Thursday | 196 |
| Friday | 201 |
| Saturday | 128 |
+-------------------+----------------------------+
Grouping by week did not solve the problem. I feel as though I need to “count where week is distinct” but I’m not sure if this is possible.
Any guidance is much appreciated, thank you!
Try to count distinct values (date part only) of your
datetimefield. More aboutCOUNT(DISTINCT expr,[expr...])andDATE(expr)that returns the date part only from datetime field.