1
votes

I have this graph where the nodes are researchers, and they are related by a relationship named R1, the relationship has a "value" property. How can I get the name of the researchers that are in the relationships with the greatest value? It's like get all the relationships order by r.value DESC but getting only the first relationship per researcher, because I don't want to see on the table duplicated researcher names. By the way, is there a way to get the name of the researchers order by the mean of their relationship "values"? Sorry about the confused topic, I don't speak English very well, thank you very much.

I've been trying things like the Cypher query bellow:

MATCH p=(n)-[r:R1]->(c) 
WHERE id(n) < id(c) and r.coauthors = false 
return DISTINCT n.name order by n.campus, r.value DESC
1
Possible duplicate of Neo4j aggregate functionTezra
What have you tried sofar?chriopp
@WolganEns It doesn't look like what? The related question contains all the information you need to answer this question, even if it's not a carbon-copy. If your question isn't answered, you will need to clarify what you are still missing.Tezra
@WolganEns Your question heavily implies that what you really want is COLLECT(r) as rs, which is an aggregate function. If you want a single sample of that collection, you can just use HEAD(COLLECT(r)) as r. Here is the cypher ref card. Everything you need is on the ref card.Tezra
@WolganEns You can use ORDER BY on an element being aggregated, and the resulting list will be sortedTezra

1 Answers

2
votes

Correct me if I am wrong, but you want one result per "n" with the highest value from "r"?

MATCH (n)-[r:R1]->(c) 
WHERE r.coauthors = false
WITH n, r ORDER BY r.value DESC
WITH n, head(collect(r)) AS highR
RETURN n.name, highR.value ORDER BY n.campus, highR.value DESC

This will get you all the r's in order and pick the first head(collect(r)) after first doing an ORDER BY. Then you just need to return the values you want. Check out Neo4j Aggregation Functions for some documentation on how aggregation functions work. Good luck!

As an aside, if there is a label that all "n" have, you should add that in your MATCH: MATCH (n:Person) .... it will help speed up your query!