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 max() function in a graph

I have graph of some nodes (Persons and the college they graduated from by [:HAS_BS_SPECIALTY_IN {Score:...}] and [:HAS_PHD_SPECIALTY_IN {Score:...}] , I want to get a person with maximum score, I use the cypher query:

MATCH (p:Person)-[r]->(s)
UNWIND r.Score AS Sc
WITH max(Sc) as x, p, r
RETURN DISTINCT p.Full_Name, x, type(r)

but it shows all the scores not the maximum one, any help?

╒═════════════════════════╤═══════╤══════════════════════╕
│"p.Full_Name" │"x" │"type(r)" │
╞═════════════════════════╪═══════╪══════════════════════╡
│Peter Adams 80 │"HAS_MS_SPECIALTY_IN" │
├─────────────────────────┼───────┼──────────────────────┤
│Peter Adams 88 │"HAS_PHD_SPECIALTY_IN"│
├─────────────────────────┼───────┼──────────────────────┤
│John Mayes 95 │"HAS_MS_SPECIALTY_IN" │
├─────────────────────────┼───────┼──────────────────────┤
│John Mayes 98 │"HAS_PHD_SPECIALTY_IN"│
├─────────────────────────┼───────┼──────────────────────┤
I just want to have John Mayes with Score: 98

1 ACCEPTED SOLUTION

There are two points: I have lots of other relations which follow the pattern (p:Person)-[r]->(s)
and also there are lots of person with an specialty but with no Score.
Your new suggestion is not working, I changed it as
MATCH (p:Person)-[r]->(s)
WHERE type(r) CONTAINS 'SPECIALTY' AND r.Score IS NOT NULL
WITH p, max(r.Score) AS score
RETURN p.Full_Name, score ORDER BY score DESC LIMIT 1

and now I got my answer.

View solution in original post

7 REPLIES 7

Hello @mustafa.sepehrian and welcome to the Neo4j community

MATCH (p:Person)-[r]->(s)
WITH p, r, r.Score AS score
ORDER BY score DESC LIMIT 1
RETURN p.Full_Name, score, type(r)

Regards,
Cobra

Sorry, not worked, where is the max() function?

Not working? Can you tell us what is the error?
You don't need to use max() function in that case normally. Is the property Score a list?

I found my answer by this query:
MATCH (p:Person)-[r]->(s)
WHERE type(r) CONTAINS 'SPECIALTY' AND r.Score IS NOT NULL
WITH p, r, r.Score AS score
RETURN p.Full_Name, score, type(r) ORDER BY score DESC Limit 1

I just had an obsession to use max() function just for learning!!

Thanks anyway.

Score is not a list, it is a property of r as relation for example: CREATE (Person41200)-[:HAS_BS_SPECIALTY_IN { Score:98}]->(Specialty0027)

Ok, can you tell where was the mistake in the query I gave you ?

You can use max():

MATCH (p:Person)-[r]->(s)
WITH p, max(r.Score) AS score
ORDER BY score DESC LIMIT 1
RETURN p.Full_Name, score

There are two points: I have lots of other relations which follow the pattern (p:Person)-[r]->(s)
and also there are lots of person with an specialty but with no Score.
Your new suggestion is not working, I changed it as
MATCH (p:Person)-[r]->(s)
WHERE type(r) CONTAINS 'SPECIALTY' AND r.Score IS NOT NULL
WITH p, max(r.Score) AS score
RETURN p.Full_Name, score ORDER BY score DESC LIMIT 1

and now I got my answer.