0
votes

I am new to cypher, and I want to get data after using 'DISTINCT', but I could only get the value of 'DISTINCT' property, for example:

CREATE (n:person {name: "a", age: 22})
CREATE (n:person {name: "a", age: 23})
CREATE (n:person {name: "a", age: 24})

I want to get only one node with label "person" whose name is "a", so I try query like this

MATCH (n:person) RETURN DISTINCT n.name

This only return "a", but I want all properties and values of the node, which is {name: "a", age:22}, what should I do?

2
I don't understand what you want as a result. Can you give an example ? Because you seems to want the distinct but also all the values ... - logisima
@logisima Take code in my question for example, I have created three person with same name but different age. I want get person info with distinct names, such as person {name: "a", age:22}, and I don't want the result contains the other two person. I hope it is more clearly... - smilebuz
OK, I understand the distinct for the name, but why you want the node age 22 instead of 23 or 24 ? Is it because it's the first, the lower, somethig else ? - logisima
@logisima Actually it doesn't matter what the age is. My real data is some insurance cases, for example, there is a case contains 2 people, so there are two records with same caseId, {caseId: 1, name:"a", time: "19:57"}, {caseId: 1, name:"b", time:"19:57"}. I don't need the "name" property, but I want "caseId" and "time". I hope I said clearly... - smilebuz

2 Answers

0
votes

You can try this query :

MATCH (n:person) 
WITH n.name, collect(n) AS persons
RETURN persons[0]

collect is an aggregate function, so in it you will have all the node aggreagted by n.name, and I return the first element.

0
votes

To get just one person node with the name "a":

MATCH (n:person {name: "a"})
RETURN n
LIMIT 1;