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

# sum (comp)

Use the `sum()` function to compute the sum of all values of a specified numeric field across all rows in each group within the `comp` stage. This is equivalent to the SQL `SUM` aggregate function.

## Syntax

```sql
| comp sum(<field>) [by <group_field1>, <group_field2>, ...] [as <alias>]
```

## Parameters

| Name          | Type    | Required | Description                                                                                       |
| ------------- | ------- | -------- | ------------------------------------------------------------------------------------------------- |
| `field`       | numeric | Yes      | The numeric field whose values will be summed.                                                    |
| `group_field` | any     | No       | One or more fields to group the results by. If omitted, all rows are treated as a single group.   |
| `alias`       | string  | No       | An alias for the output field. If not specified, the output field name defaults to `sum_<field>`. |

## Returns

**Type**: numeric

**Description**: The `sum()` function returns the total sum of all non-NULL values of the specified field within each group. Returns NULL if all values in the group are NULL. Returns 0 if the group is empty.

## Usage notes

* **Data types**: The `sum` function only works with numeric fields.
* **Null handling**: NULL values are ignored in the computation.
* **No grouping**: When used without a `by` clause, the function returns the sum across all rows.
* **Multiple aggregations**: Can be combined with other aggregation functions in the same `comp` stage.
* **Overflow**: For very large datasets with large values, be aware of potential numeric overflow.

## Examples

### Example 1: Total bytes sent per host

**Goal**: Compute the total bytes sent for each host.

**XQL code**:

```sql
dataset = xdr_data
| comp sum(bytes_sent) by agent_hostname as total_bytes_sent
```

**Explanation**: The `sum()` function adds up all `bytes_sent` values for each unique `agent_hostname`, returning the total bytes sent per host.

**Output**:

| AGENT\_HOSTNAME | TOTAL\_BYTES\_SENT |
| --------------- | ------------------ |
| workstation-1   | 1048576            |
| workstation-2   | 524288             |

### Example 2: Total events across all data

**Goal**: Compute the total sum of alert severities across all events.

**XQL code**:

```sql
dataset = xdr_data
| comp sum(alert_severity) as total_severity
```

**Explanation**: Without a `by` clause, the `sum()` function computes the total of all `alert_severity` values across the entire dataset.

**Output**:

| TOTAL\_SEVERITY |
| --------------- |
| 342             |

### Example 3: Sum with multiple aggregations

**Goal**: Compute total bytes sent and received along with event count per host.

**XQL code**:

```sql
dataset = xdr_data
| comp sum(bytes_sent) as total_sent, sum(action_network_bytes_received) as total_received, count(*) as event_count by agent_hostname
```

**Explanation**: This query combines multiple `sum()` calls with `count()` in a single `comp` stage to provide a comprehensive traffic summary per host.

**Output**:

| AGENT\_HOSTNAME | TOTAL\_SENT | TOTAL\_RECEIVED | EVENT\_COUNT |
| --------------- | ----------- | --------------- | ------------ |
| workstation-1   | 1048576     | 2097152         | 150          |
| workstation-2   | 524288      | 1048576         | 85           |

## Related articles

* **Stages**: [`comp`](/xql-command-reference-guide/readme/stages/comp.md), [`fields`](/xql-command-reference-guide/readme/stages/fields.md), [`limit`](/xql-command-reference-guide/readme/stages/limit.md)
* **Functions**: [`sum (windowcomp)`](/xql-command-reference-guide/readme/functions/sum_with_windowcomp_stage.md), [`count()`](/xql-command-reference-guide/readme/functions/count_with_comp_stage.md), [`avg()`](/xql-command-reference-guide/readme/functions/avg_with_comp_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/sum_with_comp_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.
