cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

How to get sum of properties in relationship list

xmlsysop
Node Link

Hello.

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

MATCH (c1:Company {name: ‘Apple’})-[r:hasVendor*]->(c2:Company)
RETURN c2.name, 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…

3 REPLIES 3

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 c1.name and sum(r.market 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(c1:Company{name:'Apple'})
match p = (c1)-[:hasVendor*]->(c2)
with c1, c2, [i in relationships(p) | i.marketShare] as marketShare
return c1.name, c2.name, reduce(s=0, i in marketShare | s+i ) as totalMarketShare

I realized the query could be made more compact:

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

xmlsysop
Node Link

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