Please help me writting query for this example.

19 views
Skip to first unread message

Sumit Neelam

unread,
Aug 25, 2016, 7:15:16 PM8/25/16
to Neo4j

Hi,

I am new to Neo4j and Cypher. I want to write a query for getting following information:

Graph Description: Sample graph schema image is attached in this post with properties of each node. Graph contains Professor nodes (labeled with P) and student nodes (labeled with S). Professor nodes are connected to their student nodes with edge having 'student' label. All students of same professor form a group.

Professor nodes properties:
  • name
Student nodes properties:
  • name
  • grade
Query: I want to find professors and their students who have grade greater than average grade of their group.

Example:

Students' grades avg. for professor P1 group = (7.0+8.0)/2 = 7.5 
S2 has grade greater than 7.5.
  
Students' grades avg. for professor P2 group = (4.0+8.0+9.0)/3 = 7.0
S4 and S5 have grade greater than 7.0.

Final output should be like this:

P1, S2, 8.0
P2, S4, 8.0
P2, S5, 9.0

Michael Hunger

unread,
Aug 25, 2016, 7:19:51 PM8/25/16
to ne...@googlegroups.com
simple double match

MATCH (p:Professor)<-[:STUDENT_OF]-(s:Student)
WITH p, avg(s.grade) as grade
MATCH (p:Professor)<-[:STUDENT_OF]-(s:Student)
WHERE s.grade > grade
RETURN p,collect(s) as students


better collect + filter

MATCH (p:Professor)<-[:STUDENT_OF]-(s:Student)
WITH p, avg(s.grade) as grade, collect(s) as all_students
RETURN p,[s IN all_students WHERE s.grade > grade] as students



--
You received this message because you are subscribed to the Google Groups "Neo4j" group.
To unsubscribe from this group and stop receiving emails from it, send an email to neo4j+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages