Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
06-17-2020 11:06 AM
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
Solved! Go to Solution.
06-17-2020 11:32 PM
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
06-17-2020 11:32 PM
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
06-18-2020 03:59 AM
It worked, but can you explain me what i've done wrong please?
06-18-2020 02:26 PM
What was your reasoning for the duplicated (second) MATCH statement you put in your query, or was it just a copy error?
06-18-2020 02:36 PM
Ohh, my bad, i just saw it now
All the sessions of the conference are now available online