You are looking for entities that have 'shipped
' but not 'bankpayment'
. You want to look for these values on different rows. In other words, you want to accept an entity that has a row with 'shipped
' but only when there are no rows with 'bankpayment'
.
You can do this logic in the having
clause. Well, you don't quite have a having
clause. You have a subquery with a where
clause on the outside. Although this is equivalent, I'm simplifying the query to eliminate the subquery. Then, I'm using the having
clause to check the dates and the presence/absence of the two values:
SELECT so.entity_id, so.created_at,
MAX(soe.created_at) AS shipped_at
FROM sales_order so LEFT OUTER JOIN
sales_order_entity soe
ON soe.parent_id = so.entity_id LEFT OUTER JOIN
sales_order_entity_varchar soev
ON soev.entity_id = soe.entity_id
GROUP BY so.entity_id
having MAX(soe.created_at) >= '2013-05-01 04:00:00' AND
MAX(soe.created_at) <= '2013-05-02 03:59:59' and
SUM(soev.value = 'shipped') > 0 and
sum(soev.value = 'bankpayment') = 0;