I asked a question earlier about aggregating quantities along a graph. The two answers provided worked well, but now I am trying to extend the Cypher query it to a graph of variable depth.
To summarize we start of with a bunch of leaf stores which all are associated with a particular supplier, which is a property on the Store
node. Inventory is then moved along to other stores and the proportion from each supplier corresponds to their contribution to the original store.
So for node B02
, S2
contributed 750/1250 = 60%
and S3
contributed 40%
. We then move 600 units our of B02
of which 60%
belongs to S2
and 40%
to S3
and so on.
What we want to know what percentage of the final 700 units into D01
belong to each supplier. Where suppliers with the same name are the same supplier. So for the above graph we expect:
S1, 38.09
S2, 27.61
S3, 34.28
I've prepared a graph using this Cypher script:
CREATE (A01:Store {Name: 'A01', Supplier: 'S1'})
CREATE (A02:Store {Name: 'A02', Supplier: 'S1'})
CREATE (A03:Store {Name: 'A03', Supplier: 'S2'})
CREATE (A04:Store {Name: 'A04', Supplier: 'S3'})
CREATE (A05:Store {Name: 'A05', Supplier: 'S1'})
CREATE (A06:Store {Name: 'A06', Supplier: 'S1'})
CREATE (A07:Store {Name: 'A07', Supplier: 'S2'})
CREATE (A08:Store {Name: 'A08', Supplier: 'S3'})
CREATE (B01:Store {Name: 'B01'})
CREATE (B02:Store {Name: 'B02'})
CREATE (B03:Store {Name: 'B03'})
CREATE (B04:Store {Name: 'B04'})
CREATE (C01:Store {Name: 'C01'})
CREATE (C02:Store {Name: 'C02'})
CREATE (D01:Store {Name: 'D01'})
CREATE (A01)-[:MOVE_TO {Quantity: 750}]->(B01)
CREATE (A02)-[:MOVE_TO {Quantity: 500}]->(B01)
CREATE (A03)-[:MOVE_TO {Quantity: 750}]->(B02)
CREATE (A04)-[:MOVE_TO {Quantity: 500}]->(B02)
CREATE (A05)-[:MOVE_TO {Quantity: 100}]->(B03)
CREATE (A06)-[:MOVE_TO {Quantity: 200}]->(B03)
CREATE (A07)-[:MOVE_TO {Quantity: 50}]->(B04)
CREATE (A08)-[:MOVE_TO {Quantity: 450}]->(B04)
CREATE (B01)-[:MOVE_TO {Quantity: 400}]->(C01)
CREATE (B02)-[:MOVE_TO {Quantity: 600}]->(C01)
CREATE (B03)-[:MOVE_TO {Quantity: 100}]->(C02)
CREATE (B04)-[:MOVE_TO {Quantity: 200}]->(C02)
CREATE (C01)-[:MOVE_TO {Quantity: 500}]->(D01)
CREATE (C02)-[:MOVE_TO {Quantity: 200}]->(D01)
The current query is this:
MATCH (s:Store { Name:'D01' })
MATCH (s)<-[t:MOVE_TO]-()<-[r:MOVE_TO]-(supp)
WITH t.Quantity as total, collect(r) as movements
WITH total, movements, reduce(totalSupplier = 0, r IN movements | totalSupplier + r.Quantity) as supCount
UNWIND movements as movement
RETURN startNode(movement).Supplier as Supplier, round(100.0*movement.Quantity/supCount) as pct
I am trying to use recursive relationships, something along the lines of this:
MATCH (s)<-[t:MOVE_TO]-()<-[r:MOVE_TO*]-(supp)
however that gives multiple paths to the end node and I need to aggregate the inventory at each node I think.
MATCH
, which in this case is one path across the depth of the tree. But you want to compare the paths against each otherMATCH (target:Store {Name:'D01'})<-[r:MOVE_TO*]-(source:Store) WHERE source.Supplier IS NOT NULL
WHERE NOT (source)<-[:MOVE_TO]-()