# Filtering Cypher Results

**URL:** <https://community.neo4j.com/t/filtering-cypher-results/12681>\
**Category:** Neo4j Graph Platform\
**Tags:** cypher\
**Created:** [December 8, 2019, 8:04pm UTC](https://community.neo4j.com/t/filtering-cypher-results/12681 "2019-12-08T20:04:50Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![rusthinges](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/rusthinges/32/5106_2.png) [@rusthinges](https://community.neo4j.com/u/rusthinges)\
**Post date:** [December 8, 2019, 8:04pm UTC](https://community.neo4j.com/t/filtering-cypher-results/12681/1 "2019-12-08T20:04:50Z")

</div>

I hope I am not being a bother, I have data in Neo4j and have filtered it as shown on the attached screenshot, I trying to structure the query so that I can identify cases were more than one c.names were at the same location (l.name) during the same month and year. How can I change my query to show such results?

 ![Screen Shot 2019-12-08 at 19.59.03](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/2X/a/ad142b6aee4c2e5b32f9d6602e7ae5ba96a553c0.png)

---

<div class="post-metadata">

**Author:** ![rusthinges](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/rusthinges/32/5106_2.png) [@rusthinges](https://community.neo4j.com/u/rusthinges)\
**Post date:** [December 8, 2019, 8:53pm UTC](https://community.neo4j.com/t/filtering-cypher-results/12681/2 "2019-12-08T20:53:53Z")

</div>

I am trying to filter for locations where I have more than one collector ID, i.e c.id\>1, where would I fit this in the query, all attempts so far have resulted in errors

---

<div class="post-metadata">

**Author:** ![shan](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/shan/32/7767_2.png) [@shan](https://community.neo4j.com/u/shan)\
**Post date:** [December 8, 2019, 9:31pm UTC](https://community.neo4j.com/t/filtering-cypher-results/12681/3 "2019-12-08T21:31:13Z")

</div>

```auto
MATCH (c:Event)-[r:AT_PLACE]->(l:Place) WHERE NOT r.year IS null
WITH DISTINCT l.name as location, r.Year as year, r.Month as month, COUNT(DISTINCT c) AS eventNum WHERE eventNum > 1
RETURN *

```

---

<div class="post-metadata">

**Author:** ![rusthinges](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/rusthinges/32/5106_2.png) [@rusthinges](https://community.neo4j.com/u/rusthinges)\
**Post date:** [December 8, 2019, 11:20pm UTC](https://community.neo4j.com/t/filtering-cypher-results/12681/4 "2019-12-08T23:20:57Z")

</div>

is possible to add another line to say where not r.Month is null, or does cypher allow just one such within a query

---

<div class="post-metadata">

**Author:** ![andrew\_bowman](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/andrew_bowman/32/73_2.png) [@andrew\_bowman](https://community.neo4j.com/u/andrew_bowman)\
**Post date:** [December 9, 2019, 12:21am UTC](https://community.neo4j.com/t/filtering-cypher-results/12681/5 "2019-12-09T00:21:16Z")

</div>

Sure just use an AND in your WHERE clause:

```auto
...
WHERE r.year IS NOT NULL AND r.Month IS NOT NULL
...

```

---

<div class="post-metadata">

**Author:** ![rusthinges](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/rusthinges/32/5106_2.png) [@rusthinges](https://community.neo4j.com/u/rusthinges)\
**Post date:** [December 9, 2019, 9:52pm UTC](https://community.neo4j.com/t/filtering-cypher-results/12681/6 "2019-12-09T21:52:37Z")

</div>

I worked my query around and I am now producing results that are making sense, thanks to everyone that helped, however, I have one last question

 ![Screen Shot 2019-12-09 at 21.48.02](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/2X/2/2c6b975677d9ce541ba016a171f7797a4d55fe2c.png)

Is there a way I make the query only return unique rows, for example there are multiple rows showing Jenyns Leonard, Henslow J.S , ELY, 7, 1821 and I only want to maintain one of these and remove duplicates.

The query that I used to get to this result is:  
MATCH (c:Collector)-[:COLLECTED\_WITH]-\>(c1:Collector)  
WHERE c.name \<\>c1.name AND c.Year IS NOT NULL AND c.Month IS NOT NULL AND c.name \<\>"na" AND c1.name \<\>"na"  
WITH DISTINCT c.place as location, c.Year as year, c.Month as month, c.name as Collector1, c1.name AS Collector2  
RETURN \* ORDER BY year

---

<div class="post-metadata">

**Author:** ![shan](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/shan/32/7767_2.png) [@shan](https://community.neo4j.com/u/shan)\
**Post date:** [December 9, 2019, 10:16pm UTC](https://community.neo4j.com/t/filtering-cypher-results/12681/7 "2019-12-09T22:16:51Z")

</div>

Are you ok with keeping both `c` and `c1` in one column? If so, you use this:

```auto
MATCH (c:Collector)-[:COLLECTED_WITH]->(c1:Collector)
WHERE c.name <>c1.name AND c.Year IS NOT NULL AND c.Month IS NOT NULL AND c.name <>"na" AND c1.name <>"na"
WITH DISTINCT c.place as location, c.Year as year, c.Month as month, apoc.coll.sort([c.name, c1.name]) as Collectors
RETURN * ORDER BY year

```

---

<div class="post-metadata">

**Author:** ![andrew\_bowman](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/andrew_bowman/32/73_2.png) [@andrew\_bowman](https://community.neo4j.com/u/andrew_bowman)\
**Post date:** [December 9, 2019, 10:40pm UTC](https://community.neo4j.com/t/filtering-cypher-results/12681/8 "2019-12-09T22:40:17Z")

</div>

Your query does return unique rows, you made sure of that by your usage of DISTINCT in the WITH clause before your return.

Your c and c1 may not be unique, but you can see there are differences in the month and/or year values for the row.

If you want to have unique c1 and c2, then you will need to perform aggregation for some of the remaining values, collecting month and years for example, such that the non-aggregation variables become the distinct grouping key (which will happen automatically when you perform the aggregation).

EDIT

Ah, I see what you mean with the mirrored results for c and c1.

Normally we'd recommend using something like `WHERE id(c) < id(c1)`, but that wouldn't apply to your data, as the :Collector nodes seem to represent both entity and locality and time, and not a distinct entity.

If you drop the direction (this assumes that relationships are not always reciprocal), then you can filter based on the name:

```auto
MATCH (c:Collector)-[:COLLECTED_WITH]-(c1:Collector)
WHERE c.name < c1.name ...

```

---

<div class="post-metadata">

**Author:** ![rusthinges](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/rusthinges/32/5106_2.png) [@rusthinges](https://community.neo4j.com/u/rusthinges)\
**Post date:** [December 10, 2019, 7:40pm UTC](https://community.neo4j.com/t/filtering-cypher-results/12681/9 "2019-12-10T19:40:58Z")

</div>

When I use

**MATCH (c:Collector)-[:COLLECTED\_WITH]-\>(c1:Collector)**  
**WHERE c.name \<\>c1.name AND c.Year IS NOT NULL AND c.Month IS NOT NULL AND c.name \<\>"na" AND c1.name \<\>"na"**  
**WITH DISTINCT c.place as location, c.Year as year, c.Month as month, apoc.coll.sort(c.name, c1.name) as Collectors**  
**RETURN \* ORDER BY year**

I am getting an error message

 ![Screen Shot 2019-12-10 at 19.39.34](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/2X/c/c77d5e8b270b425c5e7cdfda8d95acbe168caef9.png)

I will try and investigate

---

<div class="post-metadata">

**Author:** ![shan](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/shan/32/7767_2.png) [@shan](https://community.neo4j.com/u/shan)\
**Post date:** [December 10, 2019, 7:55pm UTC](https://community.neo4j.com/t/filtering-cypher-results/12681/10 "2019-12-10T19:55:49Z")

</div>

Put c.name and c1.name in a list and call `apoc.coll.sort([c.name, c1.name])`.  
Sorry I missed that myself in my answer.

---

<div class="post-metadata">

**Author:** ![rusthinges](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/rusthinges/32/5106_2.png) [@rusthinges](https://community.neo4j.com/u/rusthinges)\
**Post date:** [December 10, 2019, 10:18pm UTC](https://community.neo4j.com/t/filtering-cypher-results/12681/11 "2019-12-10T22:18:56Z")

</div>

can I be able to call to DISTINCTS within a statement, I think that the query below will give me the result I need if it works, however, I need to only retain the distinct Collabo and the query work to the put where I add the second distinct ..

MATCH (c:Collector)-[r:VISITED]-\>(p:Place) WHERE NOT p.name='na'AND NOT c.name='na' AND NOT r.Month IS null  
WITH c,p,r  
WITH DISTINCT p.name as location, r.Year as year, r.Month as month, c.name as Collector, apoc.coll.sort([c.name, p.name]) as Collabo, WHERE DISTINCT Collbo,  
RETURN \* ORDER BY year

---

<div class="post-metadata">

**Author:** ![shan](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/shan/32/7767_2.png) [@shan](https://community.neo4j.com/u/shan)\
**Post date:** [December 11, 2019, 4:05am UTC](https://community.neo4j.com/t/filtering-cypher-results/12681/12 "2019-12-11T04:05:24Z")

</div>

`DISTINCT` is for the whole row. You don’t need an extra `WHERE` clause.

```auto
MATCH (c:Collector)-[r:VISITED]->(p:Place) WHERE NOT p.name='na'AND NOT c.name='na' AND NOT r.Month IS null
WITH c,p,r
WITH DISTINCT p.name as location, r.Year as year, r.Month as month, c.name as Collector, apoc.coll.sort([c.name, p.name]) as Collabo 
RETURN * ORDER BY year

```
