Perfect! When we wanted to find orders from March 2017, we used the following
WHERE o.order_date >= '2017-03-01' AND o.order_date < '2017-04-01'
There is another way to write this query. Take a look:
SUM(amount) AS total_revenue
FROM orders o
JOIN customers c
ON o.customer_id = c.customer_id
WHERE o.order_date >= '20170301'
AND o.order_date < CAST('2017-03-01' AS DATE) + INTERVAL '1' month
GROUP BY c.customer_id, company_name;
This time, instead of providing the specific end date, we used the
CAST(date AS DATE) + INTERVAL 'number' interval
interval – the interval we want to add, such as
number – the amount of that interval to add.
date – the date to be modified.
In our example,
CAST('2017-03-01' AS DATE) + INTERVAL '1' month means "add one month to March 1, 2017".