1
I have in Mysql the table acesso
, that saves all client accesses to a system, it saves user id (id_usuario
) and the day (dia
) access. I am making a query to check the weekly accesses, so:
SELECT COUNT(id_usuario) AS total, dia FROM acesso
GROUP BY WEEK(dia, 1)
ORDER BY dia ASC
The problem is that it returns me strange days, some begin on Monday, others on Tuesday and often a few days repeat themselves, see an example of a return:
02/01/2019 - 08/01/2019 - 130 acessos (02/01 = quarta | 08/01 = terça)
07/01/2019 - 13/01/2019 - 165 acessos (07/01 = segunda | 13/01 = domingo)
14/01/2019 - 20/01/2019 - 163 acessos (14/01 = segunda | 20/01 = domingo)
Notice that the second result is even coming back one day.
How to make this consultation list the weeks? Because it would need a result like this (interval between Sunday to Saturday):
domingo - sábado = x acessos
domingo - sábado = x acessos
domingo - sábado = x acessos
The change of year or month does not matter. I believe that in many days would not have any access, this can be a problem to generate this way?
The way you grouped it is normal that they do not start and end always on the same day. Are you sure it is the WEEK function even you need? And you are sure that Mode 1 is the best for your case (attention to the "turn of the year" in this case) - It would be nice [Dit] the question and for an example of what you expected as output.
– Bacco
@Bacco then, there is another way to group other than by WEEK?
– caiocafardo
It depends on the result you want to get. Missing make clear in the question the desired criterion. Examples help. For example, divide the sequential number of the day by 7, group by weeks. Maybe the WEEK is good, but ai depends on the criteria of the turn of the year. If the year starts on a Tuesday, what does it do? (which will probably confuse your results with WEEK)
– Bacco
I edited! But really do not care the change of year or month, I just need to take the break from Sunday to Saturday.
– caiocafardo
The second function parameter
WEEK(dia, 1)
is informing that the weeks searched in the query always start on Monday. DoWEEK(dia, 0)
so that the weeks consulted have Sunday as the starting day.– Augusto Vasques
@Augustovasques will still have trouble grouping things from different years, and depending on where the end of the year will fall, will "separate" the turn of the year improperly. The "mode" parameter is a little more complex than just starting day setting.
– Bacco
@Bacco I would recommend to the user your answer at this time. Because I fully agree with you about the problems with the use cases of the function
WEEK()
and the way it addressed solves the user problem without creating side effects.– Augusto Vasques