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.

Use properties of relationship in Cypher query

costa0096
Node Clone

Hello.

I'm doing a neo4j project.

I'm trying to use the avg() function in a property of a relationship

This is my query:

MATCH (E1:ENTITY)-[C:CONTRACT]->(E2:ENTITY)
WITH  C.price as contractPrice
MATCH (E1:ENTITY)-[C:CONTRACT]->(E2:ENTITY)
WITH collect([E1,E2,contractPrice]) AS costRecords, avg(C.price) AS avgContractPrice
WITH [record IN costRecords WHERE record[2] > avgContractPrice] as costRecords
UNWIND costRecords as record
RETURN record[0] as E1, record[1] as E2, record[2] as cost

So what i'm trying to show is all the "node-relationship-node" graph, where the price(C.price) is greater than the average, but i get the same result as MATCH n RETURN n

What do i have to change?
Thank you all and stay safe

1 ACCEPTED SOLUTION

ameyasoft
Graph Maven
Try this:

MATCH (E1:ENTITY)-[C:CONTRACT]->(E2:ENTITY)
WITH  C.price as contractPrice, E1, E2
WITH collect([E1,E2,contractPrice]) AS costRecords, avg(contractPrice) AS avgContractPrice
WITH [record IN costRecords WHERE record[2] > avgContractPrice] as costRecords
UNWIND costRecords as record
RETURN record[0] as E1, record[1] as E2, record[2] as cost

View solution in original post

4 REPLIES 4

ameyasoft
Graph Maven
Try this:

MATCH (E1:ENTITY)-[C:CONTRACT]->(E2:ENTITY)
WITH  C.price as contractPrice, E1, E2
WITH collect([E1,E2,contractPrice]) AS costRecords, avg(contractPrice) AS avgContractPrice
WITH [record IN costRecords WHERE record[2] > avgContractPrice] as costRecords
UNWIND costRecords as record
RETURN record[0] as E1, record[1] as E2, record[2] as cost

It worked, but can you explain me what i've done wrong please?

What was your reasoning for the duplicated (second) MATCH statement you put in your query, or was it just a copy error?

Ohh, my bad, i just saw it now