# Eliminating all but last CDC record for a topic within 2 sec window

**URL:** <https://forum.confluent.io/t/eliminating-all-but-last-cdc-record-for-a-topic-within-2-sec-window/10033>\
**Category:** ksqlDB\
**Created:** [5 February 2024 11:18 UTC](https://forum.confluent.io/t/eliminating-all-but-last-cdc-record-for-a-topic-within-2-sec-window/10033 "2024-02-05T11:18:44Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![dtroiano](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.confluent.io/dtroiano/32/1961_2.png) [@dtroiano](https://forum.confluent.io/u/dtroiano)\
**Post date:** [5 February 2024 16:07 UTC](https://forum.confluent.io/t/eliminating-all-but-last-cdc-record-for-a-topic-within-2-sec-window/10033/2 "2024-02-05T16:07:24Z")

</div>

You could use a tumbling window with `EMIT FINAL` to accomplish this. E.g., if you have a stream with deduplication key column `id`, and another column that might vary called `val`, then you can get the last `val` per `id` over 2-second windows like this:

```auto
SELECT id,
    LATEST_BY_OFFSET(val),
    WINDOWSTART,
    WINDOWEND
FROM test
WINDOW TUMBLING (SIZE 2 SECONDS, GRACE PERIOD 0 SECOND)
GROUP BY id
EMIT FINAL;

```

Note that data has to keep flowing in order for windows to close (see [here](https://forum.confluent.io/t/ksqldb-table-with-tumbling-window-and-emit-final/6910/2)).

Flink SQL’s [window deduplication](https://nightlies.apache.org/flink/flink-docs-stable/docs/dev/table/sql/queries/window-deduplication/) is also a good fit for this. By ordering by timestamp `DESC` and returning the first row, you get the last row per window, e.g.:

```auto
SELECT id, val FROM (
  SELECT id,
    val,
    window_start,
    window_end,
    ROW_NUMBER() OVER (PARTITION BY id ORDER BY ts DESC) AS rownum
  FROM TABLE(TUMBLE(TABLE test, DESCRIPTOR(ts), INTERVAL '2' SECONDS))
) WHERE rownum = 1;

```

---

_[View the full topic](https://forum.confluent.io/t/eliminating-all-but-last-cdc-record-for-a-topic-within-2-sec-window/10033)._
