I am trying to select all of our items that have not been sold at all in the last 2 months.
I am trying to use this query, but it does not work as expected:
SELECT SalesDescription FROM Items I LEFT JOIN Orders_Items OI ON OI.ItemID=I.ItemID LEFT JOIN Orders O ON O.OrderID=OI.OrderID WHERE OrderTime NOT BETWEEN date_sub(curdate(), interval 2 month) AND date_sub(curdate(), interval 1 day) Group By I.ItemID
Basically, I want to get all the entries from the Items table (grouped by item ID) if and only if they have not been ordered in the last two months.
When I do my join above, the resulting table looks something like this:
Name OrderID OrderDate Widget A 1 Last Year Widget B 2 Last Week Widget C 3 Last Year Widget C 4 Last Week
Only Widget A should return my result, since it has not been ordered in the last 2 months. The fact that it was ordered more than a year ago is not relevant.
Widget C should not appear because an order containing Widget C has been placed in the last 2 months.
The problem is that the records I want will not have a date range associated with them. Another way to express this:
I want to start with all the items in the Items table, and then exclude those that have orders, and at least one of these attached orders was placed within a 2-month range.
How can i get this?
source share