> 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/median_with_windowcomp_stage.md).

# median (windowcomp)

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

## Syntax

```sql
| windowcomp median(<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           | Yes      | The numeric field from which to compute the median 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**: numeric (float)

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

## Usage notes

* **Row preservation**: Unlike `comp median()`, the `windowcomp median()` does not reduce the number of rows.
* **Percentile**: The median is the 50th percentile of the values within the window.
* **Null handling**: NULL values are ignored in the computation.
* **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`).
* **Trend analysis**: Useful for computing running medians or sliding window medians for trend analysis.

## Examples

### Example 1: Median bytes per host (all rows preserved)

**Goal**: Compute the median bytes received for each host while preserving all original rows.

**XQL code**:

```sql
dataset = xdr_data
| windowcomp median(action_network_bytes_received) by agent_hostname as median_bytes_per_host
```

**Explanation**: The `median()` function computes the median of `action_network_bytes_received` within each `agent_hostname` partition. All original rows are preserved, and each row receives the partition's median value in `median_bytes_per_host`.

**Output**:

| \_TIME              | AGENT\_HOSTNAME | ACTION\_NETWORK\_BYTES\_RECEIVED | MEDIAN\_BYTES\_PER\_HOST |
| ------------------- | --------------- | -------------------------------- | ------------------------ |
| 2024-01-15 08:00:00 | workstation-1   | 100                              | 200.0                    |
| 2024-01-15 09:00:00 | workstation-1   | 300                              | 200.0                    |
| 2024-01-15 10:00:00 | workstation-1   | 200                              | 200.0                    |
| 2024-01-15 08:30:00 | workstation-2   | 500                              | 400.0                    |
| 2024-01-15 09:30:00 | workstation-2   | 300                              | 400.0                    |

### Example 2: Running median of response times

**Goal**: Compute a running median of response times, ordered by time.

**XQL code**:

```sql
dataset = xdr_data
| windowcomp median(action_total_time) sort asc _time as running_median
```

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

**Output**:

| \_TIME              | ACTION\_TOTAL\_TIME | RUNNING\_MEDIAN |
| ------------------- | ------------------- | --------------- |
| 2024-01-15 08:00:00 | 50                  | 50.0            |
| 2024-01-15 09:00:00 | 30                  | 40.0            |
| 2024-01-15 10:00:00 | 70                  | 50.0            |
| 2024-01-15 11:00:00 | 40                  | 45.0            |

## 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**: [`median (comp)`](/xql-command-reference-guide/readme/functions/median_with_comp_stage.md), [`avg()`](/xql-command-reference-guide/readme/functions/avg_with_windowcomp_stage.md), [`max()`](/xql-command-reference-guide/readme/functions/max_with_windowcomp_stage.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/median_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.
