# How to merge results neo4j

**URL:** <https://community.neo4j.com/t/how-to-merge-results-neo4j/22167>\
**Category:** Cypher\
**Created:** [July 22, 2020, 6:28pm UTC](https://community.neo4j.com/t/how-to-merge-results-neo4j/22167 "2020-07-22T18:28:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![djole.nikolic.priv](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/djole.nikolic.priv/32/12760_2.png) [@djole.nikolic.priv](https://community.neo4j.com/u/djole.nikolic.priv)\
**Post date:** [July 22, 2020, 6:28pm UTC](https://community.neo4j.com/t/how-to-merge-results-neo4j/22167/1 "2020-07-22T18:28:28Z")

</div>

Hello people,  
I have maybe silly question but regardless..  
The query is the following:

```auto
MATCH (c:Category)<-[:CATEGORY_PERMISSION]-(p:Permission)
OPTIONAL MATCH (c)<-[:CATEGORY_STRUCTURE]-(child:Category)<-[:CATEGORY_PERMISSION]-(p2:Permission)
RETURN p, p2

```

I get category and its permissions, then I get children of the category and its permissions.  
I want to merge them together when returning and only get distinct values so I have all permissions flattened. 🙂

---

<div class="post-metadata">

**Author:** ![anthapu](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/anthapu/32/4113_2.png) [@anthapu](https://community.neo4j.com/u/anthapu)\
**Post date:** [July 23, 2020, 12:36am UTC](https://community.neo4j.com/t/how-to-merge-results-neo4j/22167/2 "2020-07-23T00:36:24Z")

</div>

If you have apoc plugin installed this is a simple task

```auto
MATCH (c:Category)<-[:CATEGORY_PERMISSION]-(p:Permission)
OPTIONAL MATCH (c)<-[:CATEGORY_STRUCTURE]-(child:Category)<-[:CATEGORY_PERMISSION]-(p2:Permission)
WITH collect(p) as firstList, collect(p2) as secondlist
WITH apoc.coll.union(firstList, secondlist) as permissions
UNWIND permissions as permission
RETURN permission

```

Here I am using unwind to make sure you get permission object as return entity rather than a list.

If you can handle the list you can return the permissions value itself.

---

<div class="post-metadata">

**Author:** ![djole.nikolic.priv](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/djole.nikolic.priv/32/12760_2.png) [@djole.nikolic.priv](https://community.neo4j.com/u/djole.nikolic.priv)\
**Post date:** [July 23, 2020, 3:16pm UTC](https://community.neo4j.com/t/how-to-merge-results-neo4j/22167/3 "2020-07-23T15:16:05Z")

</div>

What I could do is also

```auto
collect(p) + collect(p2) as all
UNWIND all as single
RETURN distinct single

```

But I guess union is cleaner since it removes distincts  
Thanks a lot
