My table has below data, and I want to get the previous data of month,and today is 2022-05-04.
1 | 2022-03-01 |
This is the answer:
1 | WITH TEMP_TABLE AS |
explain
The function DATE_TRUNC will help us convert datetime to first date of month.
It’s like Now() is 2022-05-09, and function DATE_TRUNC(‘MONTH’,NOW()) will convert datetime to 2022-05-01.I use the DATE_TRUNC create a duration.
Example:
NOW() = 2022-05-09
1 | x >= 2022-04-01 |
So,the result is 2022-04-01~2022-04-30.