<a id="flink-sql-aggregate-tumbling-window"></a>

# Aggregate a Stream in a Tumbling Window with Confluent Cloud for Apache Flink

Aggregate a continuous stream into fixed, non-overlapping intervals in
Confluent Cloud for Apache Flink® by using a *tumbling window* with the
[window table-valued functions](../reference/queries/window-tvf.md#flink-sql-window-tvfs) (Window TVFs)
SQL-standard syntax. Each event belongs to exactly one window, so you can
compute per-window minimum, maximum, average, first or last record, or
totals over an unbounded Flink SQL stream.

In this guide, you learn how to run an Flink SQL statement that
identifies the maximum and minimum orders from a continuous data stream
of orders data.

This topic shows the following steps:

- [Step 1: Inspect the example stream](#flink-sql-aggregate-tumbling-window-inspect-source)
- [Step 2: View aggregated results in a tumbling window](#flink-sql-aggregate-tumbling-window-declare-table)

## Prerequisites

You need the following prerequisites to use Flink in Confluent Cloud Console.

- Access to Confluent Cloud.

<a id="flink-sql-aggregate-tumbling-window-inspect-source"></a>

## Step 1: Inspect the example stream

In this step, you query the read-only `orders` table in the
`examples.marketplace` database to inspect the stream for fields that
you can aggregate.

1. Log in to Confluent Cloud and navigate to your Flink workspace.
2. In the **Use catalog** dropdown, select your environment.
3. In the **Use database** dropdown, select your Kafka cluster.
4. Run the following statement to inspect the example `orders` stream.
   ```sql
   SELECT * FROM examples.marketplace.orders;
   ```

   Your output should resemble:
   ```none
   order_id                                customer_id   product_id  price
   68362284-34df-41a3-87fb-50b79647b786    3195          1267        47.48
   6e03663e-d20b-4a23-848a-aec959d794e3    3094          1412        50.92
   84217b5d-7dcb-46d1-9600-675a3734a3ed    3038          1094        83.56
   ...
   ```

<a id="flink-sql-aggregate-tumbling-window-declare-table"></a>

## Step 2: View aggregated results in a tumbling window

1. Run the following statement to start a windowed query on the `orders` data.
   ```mysql
   SELECT
     window_start,
     window_end,
     MIN(price) as minimum_order_value,
     MAX(price) as maximum_order_value
   FROM TUMBLE(TABLE examples.marketplace.orders, DESCRIPTOR($rowtime), INTERVAL '10' SECOND)
   GROUP BY window_start, window_end;
   ```

   Your output should resemble:
   ```none
   window_start            window_end              minimum_order_value maximum_order_value
   2023-09-12 08:54:20.000 2023-09-12 08:54:30.000 10.05               99.75
   2023-09-12 08:54:30.000 2023-09-12 08:54:40.000 10.22               99.88
   2023-09-12 08:54:40.000 2023-09-12 08:54:50.000 10.09               150.45
   ...
   ```

The Flink statement created with this query identifies the minimum and maximum
order value in each 10-second window.

## Related content

- [Compare Current and Previous Values in a Data Stream](compare-current-and-previous-values.md#flink-sql-compare-current-and-previous-values)
- [Windowing Table-Valued Functions](../reference/queries/window-tvf.md#flink-sql-window-tvfs)
- [Window Aggregation Queries](../reference/queries/window-aggregation.md#flink-sql-window-aggregation)
- [Window Deduplication Queries](../reference/queries/window-deduplication.md#flink-sql-window-deduplication)

#### NOTE
This website includes content developed at the [Apache Software Foundation](https://www.apache.org/)
under the terms of the [Apache License v2](https://www.apache.org/licenses/LICENSE-2.0.html).
