# Trouble iterating using UNWIND

**URL:** <https://community.neo4j.com/t/trouble-iterating-using-unwind/36551>\
**Category:** Cypher\
**Tags:** cypher\
**Created:** [April 8, 2021, 5:53pm UTC](https://community.neo4j.com/t/trouble-iterating-using-unwind/36551 "2021-04-08T17:53:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![michael.temple1](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/michael.temple1/32/4308_2.png) [@michael.temple1](https://community.neo4j.com/u/michael.temple1)\
**Post date:** [April 8, 2021, 5:53pm UTC](https://community.neo4j.com/t/trouble-iterating-using-unwind/36551/1 "2021-04-08T17:53:30Z")

</div>

I am very new to Neo4j, so this is probably a simple question.  
I have several hundred nodes with a property "seq" (for sequence). This number basically represents the day of the month. So all of these several hundred nodes have a seq property between 1 and 31. I want to combine all the nodes with the same seq into a single node - so that all the nodes with seq = 1 are combined into a "January 1" node. All nodes with seq =2 are combined into a "January 2" node, etc. I have a property of "pat\_id" that will be combined into an array from all the merged noes for a day.

Here is my code:

WITH range(1,31) as counts  
UNWIND counts AS cnt  
MATCH (n:OUTPT {seq:cnt})  
WITH collect(n) AS nodes  
CALL apoc.refactor.mergeNodes(nodes, {properties: {  
pat\_id:'combine',  
seq:'discard'}, mergeRels:true})  
YIELD node  
RETURN node

I initially tried to do this with a FOREACH loop, but I can't do a MATCH inside a FOREACH.  
I have been doing an UNWIND, but it is only merging the nodes with the first value (seq = 1). I assume this is because the RETURN statement ends the loop. But when I remove the RETURN statement, I get this error:

Query cannot conclude with CALL (must be RETURN or an update clause) (line 5, column 1 (offset: 99))  
"CALL apoc.refactor.mergeNodes(nodes, {properties: {"

Any help would be appreciated.

---

<div class="post-metadata">

**Author:** ![Cobra](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/cobra/32/35243_2.png) [@Cobra](https://community.neo4j.com/u/Cobra)\
**Post date:** [April 12, 2021, 9:23pm UTC](https://community.neo4j.com/t/trouble-iterating-using-unwind/36551/2 "2021-04-12T21:23:46Z")

</div>

Hello @michael.temple1 and welcome to the Neo4j community 🙂

You were very close, you missed the `cnt` in the `WITH` clause:

```auto
WITH range(1,31) as counts
UNWIND counts AS cnt
MATCH (n:OUTPT {seq:cnt})
WITH cnt, collect(n) AS nodes
CALL apoc.refactor.mergeNodes(nodes, {properties: {
    pat_id:'combine',
    seq:'discard'
}, mergeRels:true})
YIELD node
RETURN node

```

Regards,  
Cobra

---

<div class="post-metadata">

**Author:** ![dana\_canzano](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/dana_canzano/32/27392_2.png) [@dana\_canzano](https://community.neo4j.com/u/dana_canzano)\
**Post date:** [April 12, 2021, 9:29pm UTC](https://community.neo4j.com/t/trouble-iterating-using-unwind/36551/3 "2021-04-12T21:29:15Z")

</div>

and although no a significant benefit but

```auto
WITH range(1,31) as counts
UNWIND counts AS cnt

```

could be replaced by

```auto
UNWIND range(1,31) AS cnt

```

---

<div class="post-metadata">

**Author:** ![michael.temple1](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/michael.temple1/32/4308_2.png) [@michael.temple1](https://community.neo4j.com/u/michael.temple1)\
**Post date:** [April 13, 2021, 1:06am UTC](https://community.neo4j.com/t/trouble-iterating-using-unwind/36551/4 "2021-04-13T01:06:35Z")

</div>

Thank you Cobra - this works as I want to to.  
Quick follow-up. I don't understand why I need the 'cnt' in the WITH statement. If I run the following code:

WITH range(1,31) as counts  
UNWIND counts AS cnt  
RETURN cnt

I get 31 rows back. I thought Neo4j operated on a row by row basis. So I don't quite understand why it combines all 31 rows back into a single node if I don't have 'cnt' in the WITH statement.

Also, why can you not use MATCH in a FOREACH statement? Is there something 'under the hood' in Neo4j that prevents this? Being able to MATCH in a FOREACH seems like it would make something like this much more straight-forward and easier to figure out.

Once again, thanks for your help. I really appreciate it.

---

<div class="post-metadata">

**Author:** ![Cobra](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/cobra/32/35243_2.png) [@Cobra](https://community.neo4j.com/u/Cobra)\
**Post date:** [April 13, 2021, 6:46am UTC](https://community.neo4j.com/t/trouble-iterating-using-unwind/36551/5 "2021-04-13T06:46:28Z")

</div>

Hello @michael.temple1 🙂

You must put the `cnt` variable because of the [`collect()`](https://neo4j.com/docs/cypher-manual/current/functions/aggregating/) that is an aggregation function. When you use an aggregation function, you must think by what you want to aggregate.

For [FOREACH()](https://neo4j.com/docs/cypher-manual/current/clauses/foreach/), you can only do any of these updating commands: `SET`, `REMOVE`, `CREATE`, `MERGE`, `DELETE` and `FOREACH` .

Regards,  
Cobta
