What I want to do is find the members of a region who have not bought any of the n most popular nutrients in that region.
Getting the most popular nutrients isn't hard:
MATCH (:region {reg: 'Central Cornbelt'})-[:hasDiv]->(:division)-[:hasRep]->(:Sales_rep)-[:hasCustomer]-(:customer)-[:madePurchase]->(t:transaction)
RETURN t.item_name, count(t) AS frequency
ORDER BY frequency DESC
LIMIT 3
And then it's not hard to filter by people who haven't bought any of these:
MATCH (:region {reg: 'Central Cornbelt'})-[:hasDiv]->(:division)-[:hasRep]->(:Sales_rep)-[:hasCustomer]-(c:customer)-[:madePurchase]->(t:transaction)
WHERE NOT t.item_name IN ["ITEM 1", "ITEM 2", "ITEM 3"]
RETURN DISTINCT(c.customer_name)
What I want to do is somehow combine the two, getting the list [ITEM 1...]
directly and then filtering.
I've spent half a day messing around with CALL {}
and haven't been able to get it to work.
My most recent attempt is here:
MATCH (:region {reg: 'Central Cornbelt'})-[:hasDiv]->(:division)-[:hasRep]->(:Sales_rep)-[:hasCustomer]-(c:customer)-[:madePurchase]->(t:transaction)
WHERE NOT t.item_name IN
CALL {
MATCH (:region {reg: 'Central Cornbelt'})-[:hasDiv]->(:division)-[:hasRep]->(:Sales_rep)-[:hasCustomer]-(:customer)-[:madePurchase]->(t:transaction)
RETURN t.item_name, count(t) AS frequency
ORDER BY frequency DESC
LIMIT 5
}
RETURN DISTINCT(c.customer_name)
I've also tried various uses of WITH
to sort transactions before returning them, but haven't gotten anywhere with that either.