I am using Neo4j graph database, and I am trying to build a complex cypher query
I have the following nodes and relations:
Nodes:
- Customer node
- Brand node
- Segment node (which has a "MaxReservationsPerBrandPerMonth" property)
- Reservation node
Relations:
- Each customer is segmented to a segment
- customers can have a cust_reserved relation with a reservation
- Each reservation is related to a brand
I want to get count of customers which are eligible for a new reservation for certain brand.
so, I need to count reservations for each customer for the given brand and compare with MaxReservationsPerBrandPerMonth for this customer's segment.
note that customers who don't have reservations should be counted also
Thanks for your support.
EDIT 1
after trying Michael first query, query is :
PROFILE MATCH (c:Customer)
// also count customers without reservations (make this match optional)
OPTIONAL MATCH (c)-[:CUST_RESERVED]->(r:Reservation)-[:RESERVATION_FOR]->(b:brand {BRAND_ID: "3"})
// count reservations by brand and customers
WITH c, b, count(*) as reservations
MATCH (c)-[:SEGMENTED]->(s:segment)
WHERE reservations < s.max_sms_per_month
// aggregate count customers per brand
RETURN b.NAME, count(distinct c) as customers
Query result:
the result is unexpected, I want to get eligible customer for "Honda" brand , the result is only 5 (which were already reserved for Honda before but still eligible because they didn't reach the maximum yet) , the result should be 998734 (all customers)
Query profile: Query profile
Data set: here Needed query in SQL: here
EDIT 2
Michael second query worked like a charm in around 20 sec, thank you Michael I need to include some date logic to check reservations during certain reservations please :)
:brand(BRAND_ID)
(note you should fix the spelling to:Brand(brandId)
– Michael Hunger