Neo4j get average relationships

106 views Asked by At

The following nodes relationships

(u:user{id:'some_id'})-[hpl:HAS_PRODUCT_LIST]->(pl:productList)<-[ipl:IN_PRODUCT_LIST]-(p:product)

I need to get the average products per product list.

I tried

MATCH (u:user{id: 'some_id'})-[hpl: HAS_PRODUCT_LIST]->(pl:productList)<-[ipl:IN_PRODUCT_LIST]-(p:product)
WITH count(hpl) as hplc, count(p) as pc 
RETURN pc / hplc

I'm getting a wrong calculation.

1

There are 1 answers

0
jose_bacoy On

Let's say there are three product lists plA, plB and plC. Then for plA, you have 2 products, plB has 3 products and plC has 1. Your query will give you 3 / ( 2 + 3 + 1 ) = 3 / 6 = 0.5 which is not correct. The average product count is 6/3 or 2.

Thus, your query should do the count on the product list pl and NOT on the relationship type hpl

 WITH count(pl) as plc, count(p) as pc 
 RETURN pc / plc