@Query(value = "SELECT o.restaurant_id as restaurantId, YEAR(o.date_created) as year, MONTH(o.date_created) as month, "
+ " count(1) as tpn, sum(o.total) as tpv "
+ " FROM ordersDB.orders o "
+ " WHERE o.provider_id = :providerId "
+ " AND o.status != 2"
+ " AND o.restaurant_id = :restaurantId "
+ " AND o.date_created BETWEEN :dateFrom AND :dateUntil "
+ " GROUP BY 1,2,3 ", nativeQuery = true)
List<IReportOrderEvolutionDto> getRestaurantProviderEvolution(Long providerId, Long restaurantId, LocalDate dateFrom, LocalDate dateUntil);
The problem I’ve got is that It only returns data from past months, not the current one.
If I select from 05/2023 to 10/2023, it will only return data from orders up to 09/30/2023 23:59, despite having orders created in October.
I also tried something like this:
SELECT o.restaurant_id as restaurantId, YEAR(o.date_created) as year, MONTH(o.date_created) as month,
count(1) as tpn, sum(o.total) as tpv
FROM ordersDB.orders o
WHERE o.provider_id = 1
AND o.status != 2
AND o.restaurant_id = 4
AND MONTH(o.date_created) BETWEEN MONTH(DATE('2023-05-01')) AND MONTH(DATE('2023-10-31'))
AND YEAR(o.date_created) BETWEEN YEAR(DATE('2023-05-01')) AND YEAR(DATE('2023-10-31'))
GROUP BY 1,2,3
But it didn’t work either.
Thanks a lot.
2
Answers
I see that problem with your query is that you use
MONTH()
andYEAR()
functions to compare the dates to the beginning and end of the month, respectively. It means that any orders placed in the current month will be excluded, since they will have a month value > the current month and a year value > the current year.I tried to fix the issue this way:
Let me know if it works for you, please.
If your dates are always the first day of the month you want to include, then you’ll want to compare your end value to the month following the end date, because that date exists at the boundary of your date range.