Count the weeks between two dates

5

I'm trying to perform a query between two dates that returns me the number of the week and counts progressively the week.

As an example:

Itlookslikethis:

SELECTROUND((DATEDIFF(max(atendido_em_data),min(atendido_em_data)))/7)assemana,min(atendido_em_data)asmin,max(atendido_em_data)asmaxfromger_posicao;

Idonotunderstandwhythebankkeepsreturninglikethis:

    
asked by anonymous 18.05.2018 / 22:10

2 answers

3

When you say count the weeks progressively can mean many things.

However, in the example you present in the question I did not find the meaning, because week 1 has 3 days, and days of week (mon, tue, wed) do not correspond to dates in reality (day 30 / 04 was a Monday instead of Sunday as the example suggests).

However, MySql has some date functions

a> that you can use to resolve this problem.

In this implementation, I used Week(date, mode) and passed a 'base date' to control the count sequence:

SELECT DAYNAME(data), 
       data, 
       CONCAT('semana ', WEEK(data,1) - WEEK('20180428',1) + 1) AS semana
FROM tabela

See this example working in SQL Fiddle .

I hope I have helped.

    
18.05.2018 / 23:48
4

Would the function be below? ( published in English OS )

SELECT FLOOR(DATEDIFF('2018-05-20', '2018-04-30'))/7;

To get the rounded value, as you requested in the comments:

SELECT ROUND((DATEDIFF('2018-05-20', '2018-04-30'))/7);

    
18.05.2018 / 22:12