Advanced SQL Query with dates

0

I've had a problem for days. I have my table and need to count how many grades are late, so long. I just do not know how to do this query. I need to check if they are late, to be late it has to be with a difference of 3 days from the current date and within these 3 days I need to ignore Saturdays and Sundays, anyone know how I can do this? . Thanks!

Structure of my table!

codigo (int), NomeCLiente Varchar , Data_operacao (TIMESTAMP)
    
asked by anonymous 17.01.2017 / 12:05

1 answer

1
SELECT * 
FROM minha_tabela
WHERE (
    DATEDIF(CURDATE(),Data_operacao) <=5 AND 
    WEEKDAY(Data_operacao)=5
) OR (
    DATEDIF(CURDATE(),Data_operacao) <=4 AND
    WEEKDAY(Data_operacao)=6
) OR (
    DATEDIF(CURDATE(),Data_operacao) <=3  AND 
    WEEKDAY(Data_operacao)<5)
)

Remembering that the WEEKDAY function returns:

  • 0 : Second
  • 1 : Tuesday
  • ..
  • 5 : Saturday
  • 6 : Sunday
17.01.2017 / 13:06