Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
06-24-2021 04:56 AM
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
Solved! Go to Solution.
06-24-2021 05:31 AM
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.
06-24-2021 05:02 AM
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
06-24-2021 05:08 AM
Sorry, not worked, where is the max() function?
06-24-2021 05:14 AM
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?
06-24-2021 05:19 AM
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.
06-24-2021 05:22 AM
Score is not a list, it is a property of r as relation for example: CREATE (Person41200)-[:HAS_BS_SPECIALTY_IN { Score:98}]->(Specialty0027)
06-24-2021 05:24 AM
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
06-24-2021 05:31 AM
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.
All the sessions of the conference are now available online