How do I add a date condition to my request?

I have this SQL statement. It works and I need to add one more condition. I need to sort it by date. is my date string.

SELECT dd.caption, COUNT(t.occurence) 
FROM transaction t 
  INNER JOIN dict_departments dd
    ON dd.id = t.terminal_id
GROUP BY dd.caption

      

How to add this condition:

WHERE t.occurence BETWEEN (CURRENT_DATE() - INTERVAL 1 MONTH)

      

to my request.

+3


source to share


3 answers


BETWEEN

it takes two arguments, a start point and an end point. If the end point is the current time, you have two options:

  • Usage BETWEEN

    :

WHERE t.occurence BETWEEN (CURRENT_DATE() - INTERVAL 1 MONTH) AND NOW()



  1. Using a simple comparison operator:

WHERE t.occurence >= (CURRENT_DATE() - INTERVAL 1 MONTH)

0


source


Try the following:

WHERE t.occurrece BETWEEN current_date() AND dateadd(month,1,current_date())

      



The dateadd function is a SQL SERVER function, but the rest of the statement is standard SQL.

+3


source


If you want to filter dates from 1 month to present:

WHERE (t.occurrece BETWEEN DATE_ADD(CURDATE(), INTERVAL -1 MONTH) AND CURDATE()) = 1

      

or

WHERE (t.occurrece BETWEEN ADDATE(CURDATE(), INTERVAL -1 MONTH) AND CURDATE()) = 1

      

0


source







All Articles