3

I have this SQL statement. It works, and I need to add another one condition. I need to sort it by date. occurence - is my date row.

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 query.

shA.t
  • 16,580
  • 5
  • 54
  • 111
Шыназ Алиш
  • 401
  • 2
  • 7
  • 23

3 Answers3

3

Try this:

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

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

Pieter Geerkens
  • 11,775
  • 2
  • 32
  • 52
0

If you want to filter dates from 1 month ago till now:

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
shA.t
  • 16,580
  • 5
  • 54
  • 111
0

BETWEEN requires two arguments, a start point and an end point. If your end point is the current time, you have two options:

  1. Using BETWEEN:

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

  1. Using simple comparison operator:

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

Anthony
  • 36,459
  • 25
  • 97
  • 163