# Combining Consecutive Same Value Rows

**URL:** <https://forum.knime.com/t/combining-consecutive-same-value-rows/6697>\
**Category:** KNIME Analytics Platform\
**Created:** [September 22, 2014, 9:26pm UTC](https://forum.knime.com/t/combining-consecutive-same-value-rows/6697 "2014-09-22T21:26:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![muhammadm](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/m/9fc29f/32.png) [@muhammadm](https://forum.knime.com/u/muhammadm)\
**Post date:** [September 22, 2014, 9:26pm UTC](https://forum.knime.com/t/combining-consecutive-same-value-rows/6697/1 "2014-09-22T21:26:40Z")

</div>

Hi I am trying to combine consecutive same value rows however I am unable to find a reasonable method to do so.

&nbsp;

Example:

| 159291 |
| 440348 |
| 81549 |
| 485128 |
| 439796 |
| 138031 |
| 439846 |
| 1438 |
| 62093 |
| 472 |
| 472 |
| 472 |
| 472 |
| 472 |
| 472 |
| 470 |
| 470 |
| 470 |
| 470 |
| 760 |
| 82611 |
| 161 |
| 2683871 |
| 280689 |
| 288931 |
| 472 |
| 280533 |
| 280974 |
| 281031 |
| 163 |

&nbsp;

I need to maintain the ones that are by them self, for example 472 should appear twice, once after 62093 and once after 288931. Is there an easy workflow to acheive this?

Thanks.

---

<div class="post-metadata">

**Author:** ![Ellert\_van\_Koperen](https://forum-cdn.knime.com/user_avatar/forum.knime.com/ellert_van_koperen/32/12490_2.png) [@Ellert\_van\_Koperen](https://forum.knime.com/u/Ellert_van_Koperen)\
**Post date:** [September 23, 2014, 10:19am UTC](https://forum.knime.com/t/combining-consecutive-same-value-rows/6697/2 "2014-09-23T10:19:40Z")

</div>

You could also solve it with the Java Snippet node.

Connect a Java Snippet node and in the global declaration field define a field "lastKey" (Int, last seen Key) and "Use" (int, use this value). In the method body field, define a script such as:

_if (!$Key$.equals(lastKey)) {  
&nbsp; Use = 1;  
} else {  
&nbsp; Use = 0;  
}  
lastKey = $Key$;  
return Use;_

The return type of the script is an integer. You can add this column and then filter on it.

---

<div class="post-metadata">

**Author:** ![Iris](https://forum-cdn.knime.com/user_avatar/forum.knime.com/iris/32/14_2.png) [@Iris](https://forum.knime.com/u/Iris)\
**Post date:** [September 23, 2014, 1:43pm UTC](https://forum.knime.com/t/combining-consecutive-same-value-rows/6697/3 "2014-09-23T13:43:12Z")

</div>

Hi,

you can do this using the Moving Aggregation node (which is my new favourite node, thanks to Tobias :)).

See the attached workflow. The idea is to use a window of size 2 and count the Unique Values in it. Next filter all rows which got only one unique value in this window of size 2.

Cheers, Iris

- [consecutiverows.zip](https://www.knime.com/sites/default/files/consecutiverows.zip)

---

<div class="post-metadata">

**Author:** ![system](https://forum-cdn.knime.com/uploads/default/original/1X/ab3ccf34482a0329361734a18199390177204f15.png) [@system](https://forum.knime.com/u/system)\
**Post date:** [June 23, 2023, 8:23pm UTC](https://forum.knime.com/t/combining-consecutive-same-value-rows/6697/4 "2023-06-23T20:23:36Z")

</div>


