> For the complete documentation index, see [llms.txt](https://cortex-docs.paloaltonetworks.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://cortex-docs.paloaltonetworks.com/xql-command-reference-guide/readme/functions/max_with_windowcomp_stage.md).

# max (windowcomp)

Use the `max()` function within the `windowcomp` stage to compute the maximum value of a specified field over a window of rows. Unlike the `comp` stage version, the `windowcomp` version preserves all original rows and adds the computed maximum as a new field. This is equivalent to `MAX() OVER(...)` in SQL.

## Syntax

```sql
| windowcomp max(<field>) [by <partition_field1>, <partition_field2>, ...] [sort [asc|desc] <sort_field1>, ...] [between <lower> [and <upper>] [frame_type=rows|range]] [as <alias>]
```

## Parameters

| Name              | Type                         | Required | Description                                                                                                           |
| ----------------- | ---------------------------- | -------- | --------------------------------------------------------------------------------------------------------------------- |
| `field`           | numeric, string, or datetime | Yes      | The field from which to compute the maximum value.                                                                    |
| `partition_field` | any                          | No       | One or more fields to partition the data by (equivalent to SQL `PARTITION BY`).                                       |
| `sort_field`      | any                          | No       | One or more fields to define the order within each partition (equivalent to SQL `ORDER BY`). Defaults to ascending.   |
| `lower`           | integer or `null`            | No       | Lower bound of the window frame. `0` = current row, negative = rows before, `null` = unbounded.                       |
| `upper`           | integer or `null`            | No       | Upper bound of the window frame. `0` = current row, positive = rows after, `null` = unbounded.                        |
| `frame_type`      | `rows` or `range`            | No       | Type of window frame. `rows` (default) uses physical row offsets; `range` uses value-based offsets on the sort field. |
| `alias`           | string                       | No       | An alias for the output field.                                                                                        |

## Returns

**Type**: same as input field

**Description**: The `max()` function returns the maximum value within the defined window for each row. All original rows are preserved.

## Usage notes

* **Row preservation**: Unlike `comp max()`, the `windowcomp max()` does not reduce the number of rows. Each row retains its original data and gets an additional field with the window maximum.
* **No partitioning**: If no `by` clause is specified, the window spans the entire result set.
* **Default frame**: If no window frame is specified, the default frame is from the start of the partition to the current row (`between null and 0`).
* **Null handling**: NULL values are ignored in the computation.
* **Running maximum**: Can be combined with `sort` to compute running maximums.

## Examples

### Example 1: Maximum severity per host (all rows preserved)

**Goal**: Compute the maximum alert severity for each host while preserving all original rows.

**XQL code**:

```sql
dataset = xdr_data
| windowcomp max(alert_severity) by agent_hostname as max_severity_per_host
```

**Explanation**: The `max()` function computes the maximum `alert_severity` within each `agent_hostname` partition. All original rows are preserved, and each row receives the partition's maximum value in `max_severity_per_host`.

**Output**:

| \_TIME              | AGENT\_HOSTNAME | ALERT\_SEVERITY | MAX\_SEVERITY\_PER\_HOST |
| ------------------- | --------------- | --------------- | ------------------------ |
| 2024-01-15 08:00:00 | workstation-1   | 3               | 7                        |
| 2024-01-15 09:00:00 | workstation-1   | 7               | 7                        |
| 2024-01-15 10:00:00 | workstation-1   | 5               | 7                        |
| 2024-01-15 08:30:00 | workstation-2   | 2               | 4                        |
| 2024-01-15 09:30:00 | workstation-2   | 4               | 4                        |

### Example 2: Running maximum over time

**Goal**: Compute a running maximum of bytes sent, ordered by time.

**XQL code**:

```sql
dataset = xdr_data
| windowcomp max(bytes_sent) sort asc _time as running_max_bytes
```

**Explanation**: By sorting on `_time` in ascending order, the `max()` function computes a running maximum of `bytes_sent` from the start of the dataset up to the current row.

**Output**:

| \_TIME              | BYTES\_SENT | RUNNING\_MAX\_BYTES |
| ------------------- | ----------- | ------------------- |
| 2024-01-15 08:00:00 | 100         | 100                 |
| 2024-01-15 09:00:00 | 250         | 250                 |
| 2024-01-15 10:00:00 | 150         | 250                 |
| 2024-01-15 11:00:00 | 300         | 300                 |

### Example 3: Maximum within a sliding window of 3 rows

**Goal**: Compute the maximum bytes sent within a sliding window of 3 rows (current row plus one row before and one row after).

**XQL code**:

```sql
dataset = xdr_data
| windowcomp max(bytes_sent) sort asc _time between -1 and 1 as local_max
```

**Explanation**: The `between -1 and 1` clause defines a sliding window of 3 rows centered on the current row. The `max()` function returns the maximum `bytes_sent` value within that window for each row.

**Output**:

| \_TIME              | BYTES\_SENT | LOCAL\_MAX |
| ------------------- | ----------- | ---------- |
| 2024-01-15 08:00:00 | 100         | 250        |
| 2024-01-15 09:00:00 | 250         | 250        |
| 2024-01-15 10:00:00 | 150         | 300        |
| 2024-01-15 11:00:00 | 300         | 300        |

## Related articles

* **Stages**: [`windowcomp`](/xql-command-reference-guide/readme/stages/windowcomp.md), [`comp`](/xql-command-reference-guide/readme/stages/comp.md), [`sort`](/xql-command-reference-guide/readme/stages/sort.md)
* **Functions**: [`max (comp)`](/xql-command-reference-guide/readme/functions/max_with_comp_stage.md), [`min()`](/xql-command-reference-guide/readme/functions/min_with_windowcomp_stage.md), [`greatest()`](/xql-command-reference-guide/readme/functions/greatest.md)
* **Datasets**: [`xdr_data`](https://docs-cortex.paloaltonetworks.com/r/Cortex-XQL-Schema-Reference-Guide/Introduction)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://cortex-docs.paloaltonetworks.com/xql-command-reference-guide/readme/functions/max_with_windowcomp_stage.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
