1

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
Wolgan Ens
  • 385
  • 1
  • 11
  • Possible duplicate of [Neo4j aggregate function](https://stackoverflow.com/questions/43867270/neo4j-aggregate-function) – Tezra Nov 12 '18 at 20:12
  • What have you tried sofar? – chriopp Nov 12 '18 at 20:35
  • @GuyCoder fair enough, I will try to get better on this aspect. – Wolgan Ens Nov 12 '18 at 20:51
  • @Tezra it doesn't look like... – Wolgan Ens Nov 12 '18 at 20:51
  • @chriopp I tried things like: 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 – Wolgan Ens Nov 12 '18 at 20:52
  • Please take a look at https://stackoverflow.com/help/how-to-ask. Details in the comments don't do the job. – chriopp Nov 12 '18 at 20:58
  • 1
    @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 Nov 12 '18 at 21:06
  • @Tezra I think that with the aggregation functions I could add a property with the mean I mentioned in the question, but I don't see how to get only the first relation of each node. – Wolgan Ens Nov 12 '18 at 21:20
  • 1
    @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](https://neo4j.com/docs/cypher-refcard/current/). Everything you need is on the ref card. – Tezra Nov 12 '18 at 21:27
  • @Tezra Thank you for answering, but that way I'll have exactly one result, right? sorry if I'm wrong, I am very new to this and it's only a part of a task that I need to be in neo4j. I need to get the first relationship but from all the nodes. That is, the first relationship of each node order by r.value so I can get the more "valuable" relationship from each node. – Wolgan Ens Nov 12 '18 at 21:52
  • 1
    @WolganEns You can use ORDER BY on an element being aggregated, and the resulting list will be sorted – Tezra Nov 12 '18 at 21:56
  • @Tezra ok, I'll try something, thank you very much. – Wolgan Ens Nov 12 '18 at 21:59

1 Answers1

2

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!

jacob.mccrumb
  • 681
  • 5
  • 18
  • Yes, one result per "n" with the highest value from r. I just changed the r.value in the return statement to highR.value and I think it's done. Thank you. – Wolgan Ens Nov 13 '18 at 18:53