How to get sum of properties in relationship list


Can I get sum of properties in relationship list?
For example,

MATCH (c1:Company {name: ‘Apple’})-[r:hasVendor*]->(c2:Company)
RETURN, sum(r.marketShare);

But it makes error ‘type mismatch’ as blow.

Type mismatch: expected Map, Node, Relationship, Point, Duration, Date, Time, LocalTime, LocalDateTime or DateTime but was List<Relationship>

Is it possible to get sum of properties in List?
Please, help me…

Yes you can. In your case you have a variable length pattern, which is the cause of the issue. It should return an answer if you remove the ‘*’, but the result will be a list of c2 companies and the sum will just be the market share between company c1 and c2. If you want the sum of the market share across all companies c2 for a given company c1, then try returning and sum( share)

Do you need the variable length pattern in your solution? If so, are you looking for the sum of market shares along the path between a company c1 and c2, outputting the value for each company c2?

The following query will give you the sum along the path:

match p = (c1)-[:hasVendor*]->(c2)
with c1, c2, [i in relationships(p) | i.marketShare] as marketShare
return,, reduce(s=0, i in marketShare | s+i ) as totalMarketShare

I realized the query could be made more compact:

match p = (c1)-[:hasVendor*]->(c2)
return,, reduce(s=0, i in relationships(p) | s+i.marketShare ) as totalMarketShare

You saved me from trouble. (: I really appreciate your help.