Search code examples
neo4jcypher

How to remove duplicate rows in neo4j


I have a line in my query in NEO4j using cypher:

"WITH *, collect(human.name) as  sample"

Which produces results such as:

Answer
Bob
Sally
Bob
Bob
Sally

And I would like to remove duplicates such that the answer produces:

Answer
Bob
Sally

How can i go about doing this? The line below achieves my desired result but it does not let me continue writing further query lines after the RETURN as "RETURN can only be used at the last line of the query".

RETURN DISTINCT sample

Solution

  • As you mentioned DISTINCT will do the job, Use:

    WITH collect(DISTINCT human.name) as sample
    

    For example, with sample data:

    MERGE (a:Human{name: "Bob", key: 1})
    MERGE (c:Human{name: "Sally", key: 2})
    MERGE (b:Human{name: "Bob", key: 3})
    MERGE (d:Human{name: "Bob", key: 4})
    MERGE (e:Human{name: "Sally", key: 5})
    

    And query:

    MATCH (human:Human)
    WITH COLLECT (DISTINCT human.name) as sample
    return sample
    

    The response is:

    ╒═══════════════╕
    │"sample"       │
    ╞═══════════════╡
    │["Bob","Sally"]│
    └───────────────┘