cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Using Distinct in a standarized query

I have a program that expects a very simple output from Neo4j in the form of m,d,p

so this statement works fine.
MATCH (p:ProgNode {compileunit:'Way-Duy_User-Task-Manager', release:2})-[d:RUNS]->(m) RETURN m, d, p

But I have a situation where I need to display that information for each unique occurance of compileunit. I am trying to build one that uses a distinct directive (e.g. distinct (p.compileunit)) but I cannot figure out how to embed it so I maintain the output (m,d,p) the program is expecting.

Thanks

1 ACCEPTED SOLUTION

First you have to move the p element as your first grouping key, like this:

MATCH (p:ProgNode {compileunit:'Way-Duy_User-Task-Manager', release:2})-[d:RUNS]->(m) RETURN p.compileunit,d,m

After you have to put the operator DISTINCT like this

MATCH (p:ProgNode {compileunit:'Way-Duy_User-Task-Manager', release:2})-[d:RUNS]->(m) RETURN DISTINCT p.compileunit,d,m

And finally, you have to aggregate d and m as there are more than one for a distinct p.compileunit and so they can appear on a single row. If you don't DISTINCT operator is useless and will be ignore by Neo4j.

When you RETURN your query result, do not forget that there is only 2 dimensions in a RETURN clause table unlike Neo4j where this is an unlimited number of connections and so dimensions.

MATCH (p:ProgNode {compileunit:'Way-Duy_User-Task-Manager', release:2})-[d:RUNS]->(m) RETURN DISTINCT p.compileunit,collect(d),collect(m)

There is a lot of ways to aggregate your data it's up to you but if you want one single row per p.compileunit you have to do it.

View solution in original post

2 REPLIES 2

First you have to move the p element as your first grouping key, like this:

MATCH (p:ProgNode {compileunit:'Way-Duy_User-Task-Manager', release:2})-[d:RUNS]->(m) RETURN p.compileunit,d,m

After you have to put the operator DISTINCT like this

MATCH (p:ProgNode {compileunit:'Way-Duy_User-Task-Manager', release:2})-[d:RUNS]->(m) RETURN DISTINCT p.compileunit,d,m

And finally, you have to aggregate d and m as there are more than one for a distinct p.compileunit and so they can appear on a single row. If you don't DISTINCT operator is useless and will be ignore by Neo4j.

When you RETURN your query result, do not forget that there is only 2 dimensions in a RETURN clause table unlike Neo4j where this is an unlimited number of connections and so dimensions.

MATCH (p:ProgNode {compileunit:'Way-Duy_User-Task-Manager', release:2})-[d:RUNS]->(m) RETURN DISTINCT p.compileunit,collect(d),collect(m)

There is a lot of ways to aggregate your data it's up to you but if you want one single row per p.compileunit you have to do it.

Thank you. That made sense and worked. Thanks again