Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,8 @@ Gemfile.lock
.jekyll-cache
.project
vendor/bundle
node_modules
.vscode
.ruby-version
cdk*
.dev*
2 changes: 1 addition & 1 deletion _dashboards/management/S3-data-source.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,5 @@ This feature is currently under development, including the data integration func

- Learn about [querying your data in Data Explorer]({{site.url}}{{site.baseurl}}/dashboards/management/query-data-source/) through OpenSearch Dashboards.
- Learn about [optimizing the query performance of your external data sources]({{site.url}}{{site.baseurl}}/dashboards/management/accelerate-external-data/), such as Amazon S3, through Query Workbench.
- Learn about Amazon S3 and AWS Glue Data Catalog and the APIs used with Amazon S3 data sources, including configuration settings and query examples in the [PPL documentation]({{site.url}}{{site.baseurl}}/sql-and-ppl/ppl/).
- Learn about [Amazon S3 and AWS Glue Data Catalog](https://github.com/opensearch-project/sql/blob/main/docs/user/ppl/admin/connectors/s3glue_connector.md) and the APIs used with Amazon S3 data sources, including configuration settings and query examples.
- Learn about [managing your indexes]({{site.url}}{{site.baseurl}}/dashboards/im-dashboards/index/) through OpenSearch Dashboards.
157 changes: 157 additions & 0 deletions _sql-and-ppl/ppl/commands/ad.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
---
layout: default
title: ad
parent: Commands
grand_parent: PPL
nav_order: 1
---

# ad (Deprecated)

Check failure on line 9 in _sql-and-ppl/ppl/commands/ad.md

View workflow job for this annotation

GitHub Actions / vale

[vale] _sql-and-ppl/ppl/commands/ad.md#L9

[OpenSearch.HeadingCapitalization] 'ad (Deprecated)' is a heading and should be in sentence case.
Raw output
{"message": "[OpenSearch.HeadingCapitalization] 'ad (Deprecated)' is a heading and should be in sentence case.", "location": {"path": "_sql-and-ppl/ppl/commands/ad.md", "range": {"start": {"line": 9, "column": 3}}}, "severity": "ERROR"}

The `ad` command is deprecated in favor of the [`ml` command]({{site.url}}{{site.baseurl}}/sql-and-ppl/ppl/commands/ml/).
{: .warning}

The `ad` command applies the Random Cut Forest (RCF) algorithm in the ML Commons plugin to the search results returned by a PPL command. The command provides two anomaly detection approaches:

- [Anomaly detection for time-series data](#anomaly-detection-for-time-series-data) using the fixed-in-time RCF algorithm
- [Anomaly detection for non-time-series data](#anomaly-detection-for-non-time-series-data) using the batch RCF algorithm

To use the `ad` command, `plugins.calcite.enabled` must be set to `false`.
{: .note}

## Syntax

The `ad` command has two different syntax variants, depending on the algorithm type.

### Anomaly detection for time-series data

Use this syntax to detect anomalies in time-series data. This method uses the fixed-in-time RCF algorithm, which is optimized for sequential data patterns.

The fixed-in-time RCF `ad` command has the following syntax:

```sql
ad [number_of_trees] [shingle_size] [sample_size] [output_after] [time_decay] [anomaly_rate] <time_field> [date_format] [time_zone] [category_field]
```

### Parameters

The fixed-in-time RCF algorithm supports the following parameters.

| Parameter | Required/Optional | Description |
| --- | --- | --- |
| `time_field` | Required | The time field for RCF to use as time-series data. |
| `number_of_trees` | Optional | The number of trees in the forest. Default is `30`. |
| `shingle_size` | Optional | The number of records in a shingle. A shingle is a consecutive sequence of the most recent records. Default is `8`. |
| `sample_size` | Optional | The sample size used by the stream samplers in this forest. Default is `256`. |
| `output_after` | Optional | The number of points required by the stream samplers before results are returned. Default is `32`. |
| `time_decay` | Optional | The decay factor used by the stream samplers in this forest. Default is `0.0001`. |
| `anomaly_rate` | Optional | The anomaly rate. Default is `0.005`. |
| `date_format` | Optional | The format used for the `time_field` field. Default is `yyyy-MM-dd HH:mm:ss`. |
| `time_zone` | Optional | The time zone for the `time_field` field. Default is `UTC`. |
| `category_field` | Optional | The category field used to group input values. The predict operation is applied to each category independently. |

Check failure on line 51 in _sql-and-ppl/ppl/commands/ad.md

View workflow job for this annotation

GitHub Actions / vale

[vale] _sql-and-ppl/ppl/commands/ad.md#L51

[OpenSearch.TableHeadings] 'category' is a table heading and should be in sentence case.
Raw output
{"message": "[OpenSearch.TableHeadings] 'category' is a table heading and should be in sentence case.", "location": {"path": "_sql-and-ppl/ppl/commands/ad.md", "range": {"start": {"line": 51, "column": 4}}}, "severity": "ERROR"}


### Anomaly detection for non-time-series data

Use this syntax to detect anomalies in data where the order doesn't matter. This method uses the batch RCF algorithm, which is optimized for independent data points.

The batch RCF `ad` command has the following syntax:

```sql
ad [number_of_trees] [sample_size] [output_after] [training_data_size] [anomaly_score_threshold] [category_field]

Check failure on line 61 in _sql-and-ppl/ppl/commands/ad.md

View workflow job for this annotation

GitHub Actions / vale

[vale] _sql-and-ppl/ppl/commands/ad.md#L61

[OpenSearch.TableHeadings] 'category' is a table heading and should be in sentence case.
Raw output
{"message": "[OpenSearch.TableHeadings] 'category' is a table heading and should be in sentence case.", "location": {"path": "_sql-and-ppl/ppl/commands/ad.md", "range": {"start": {"line": 61, "column": 99}}}, "severity": "ERROR"}
```

### Parameters

The batch RCF algorithm supports the following parameters.

| Parameter | Required/Optional | Description |
| --- | --- | --- |
| `number_of_trees` | Optional | The number of trees in the forest. Default is `30`. |
| `sample_size` | Optional | The number of random samples provided to each tree from the training dataset. Default is `256`. |
| `output_after` | Optional | The number of points required by the stream samplers before results are returned. Default is `32`. |
| `training_data_size` | Optional | The size of the training dataset. Default is the full dataset size. |
| `anomaly_score_threshold` | Optional | The anomaly score threshold. Default is `1.0`. |
| `category_field` | Optional | The category field used to group input values. The predict operation is applied to each category independently. |


## Example 1: Detecting events in New York City taxi ridership time-series data

Check failure on line 78 in _sql-and-ppl/ppl/commands/ad.md

View workflow job for this annotation

GitHub Actions / vale

[vale] _sql-and-ppl/ppl/commands/ad.md#L78

[OpenSearch.HeadingCapitalization] 'Example 1: Detecting events in New York City taxi ridership time-series data' is a heading and should be in sentence case.
Raw output
{"message": "[OpenSearch.HeadingCapitalization] 'Example 1: Detecting events in New York City taxi ridership time-series data' is a heading and should be in sentence case.", "location": {"path": "_sql-and-ppl/ppl/commands/ad.md", "range": {"start": {"line": 78, "column": 4}}}, "severity": "ERROR"}

The following examples use the `nyc_taxi` dataset, which contains New York City taxi ridership data with fields including `value` (number of rides), `timestamp` (time of measurement), and `category` (time period classifications such as 'day' and 'night').

This example trains an RCF model and uses it to detect anomalies in time-series ridership data:

```sql
source=nyc_taxi
| fields value, timestamp
| AD time_field='timestamp'
| where value=10844.0
```
{% include copy.html %}

The query returns the following results:

| value | timestamp | score | anomaly_grade |

Check failure on line 94 in _sql-and-ppl/ppl/commands/ad.md

View workflow job for this annotation

GitHub Actions / vale

[vale] _sql-and-ppl/ppl/commands/ad.md#L94

[OpenSearch.TableHeadings] 'value' is a table heading and should be in sentence case.
Raw output
{"message": "[OpenSearch.TableHeadings] 'value' is a table heading and should be in sentence case.", "location": {"path": "_sql-and-ppl/ppl/commands/ad.md", "range": {"start": {"line": 94, "column": 3}}}, "severity": "ERROR"}

Check failure on line 94 in _sql-and-ppl/ppl/commands/ad.md

View workflow job for this annotation

GitHub Actions / vale

[vale] _sql-and-ppl/ppl/commands/ad.md#L94

[OpenSearch.TableHeadings] 'timestamp' is a table heading and should be in sentence case.
Raw output
{"message": "[OpenSearch.TableHeadings] 'timestamp' is a table heading and should be in sentence case.", "location": {"path": "_sql-and-ppl/ppl/commands/ad.md", "range": {"start": {"line": 94, "column": 11}}}, "severity": "ERROR"}

Check failure on line 94 in _sql-and-ppl/ppl/commands/ad.md

View workflow job for this annotation

GitHub Actions / vale

[vale] _sql-and-ppl/ppl/commands/ad.md#L94

[OpenSearch.TableHeadings] 'score' is a table heading and should be in sentence case.
Raw output
{"message": "[OpenSearch.TableHeadings] 'score' is a table heading and should be in sentence case.", "location": {"path": "_sql-and-ppl/ppl/commands/ad.md", "range": {"start": {"line": 94, "column": 23}}}, "severity": "ERROR"}
| --- | --- | --- | --- |
| 10844.0 | 2014-07-01 00:00:00 | 0.0 | 0.0 |

Check failure on line 96 in _sql-and-ppl/ppl/commands/ad.md

View workflow job for this annotation

GitHub Actions / vale

[vale] _sql-and-ppl/ppl/commands/ad.md#L96

[OpenSearch.Range] Use an en dash (--) with no space on either side in a range of numbers.
Raw output
{"message": "[OpenSearch.Range] Use an en dash (--) with no space on either side in a range of numbers.", "location": {"path": "_sql-and-ppl/ppl/commands/ad.md", "range": {"start": {"line": 96, "column": 13}}}, "severity": "ERROR"}


## Example 2: Detecting events in New York City taxi ridership time-series data by category

Check failure on line 99 in _sql-and-ppl/ppl/commands/ad.md

View workflow job for this annotation

GitHub Actions / vale

[vale] _sql-and-ppl/ppl/commands/ad.md#L99

[OpenSearch.HeadingCapitalization] 'Example 2: Detecting events in New York City taxi ridership time-series data by category' is a heading and should be in sentence case.
Raw output
{"message": "[OpenSearch.HeadingCapitalization] 'Example 2: Detecting events in New York City taxi ridership time-series data by category' is a heading and should be in sentence case.", "location": {"path": "_sql-and-ppl/ppl/commands/ad.md", "range": {"start": {"line": 99, "column": 4}}}, "severity": "ERROR"}

This example trains an RCF model and uses it to detect anomalies in time-series ridership data across multiple category values:

```sql
source=nyc_taxi
| fields category, value, timestamp
| AD time_field='timestamp' category_field='category'
| where value=10844.0 or value=6526.0
```
{% include copy.html %}

The query returns the following results:

| category | value | timestamp | score | anomaly_grade |

Check failure on line 113 in _sql-and-ppl/ppl/commands/ad.md

View workflow job for this annotation

GitHub Actions / vale

[vale] _sql-and-ppl/ppl/commands/ad.md#L113

[OpenSearch.TableHeadings] 'value' is a table heading and should be in sentence case.
Raw output
{"message": "[OpenSearch.TableHeadings] 'value' is a table heading and should be in sentence case.", "location": {"path": "_sql-and-ppl/ppl/commands/ad.md", "range": {"start": {"line": 113, "column": 14}}}, "severity": "ERROR"}

Check failure on line 113 in _sql-and-ppl/ppl/commands/ad.md

View workflow job for this annotation

GitHub Actions / vale

[vale] _sql-and-ppl/ppl/commands/ad.md#L113

[OpenSearch.TableHeadings] 'timestamp' is a table heading and should be in sentence case.
Raw output
{"message": "[OpenSearch.TableHeadings] 'timestamp' is a table heading and should be in sentence case.", "location": {"path": "_sql-and-ppl/ppl/commands/ad.md", "range": {"start": {"line": 113, "column": 22}}}, "severity": "ERROR"}

Check failure on line 113 in _sql-and-ppl/ppl/commands/ad.md

View workflow job for this annotation

GitHub Actions / vale

[vale] _sql-and-ppl/ppl/commands/ad.md#L113

[OpenSearch.TableHeadings] 'score' is a table heading and should be in sentence case.
Raw output
{"message": "[OpenSearch.TableHeadings] 'score' is a table heading and should be in sentence case.", "location": {"path": "_sql-and-ppl/ppl/commands/ad.md", "range": {"start": {"line": 113, "column": 34}}}, "severity": "ERROR"}
| --- | --- | --- | --- | --- |
| night | 10844.0 | 2014-07-01 00:00:00 | 0.0 | 0.0 |

Check failure on line 115 in _sql-and-ppl/ppl/commands/ad.md

View workflow job for this annotation

GitHub Actions / vale

[vale] _sql-and-ppl/ppl/commands/ad.md#L115

[OpenSearch.Range] Use an en dash (--) with no space on either side in a range of numbers.
Raw output
{"message": "[OpenSearch.Range] Use an en dash (--) with no space on either side in a range of numbers.", "location": {"path": "_sql-and-ppl/ppl/commands/ad.md", "range": {"start": {"line": 115, "column": 21}}}, "severity": "ERROR"}
| day | 6526.0 | 2014-07-01 06:00:00 | 0.0 | 0.0 |

Check failure on line 116 in _sql-and-ppl/ppl/commands/ad.md

View workflow job for this annotation

GitHub Actions / vale

[vale] _sql-and-ppl/ppl/commands/ad.md#L116

[OpenSearch.Range] Use an en dash (--) with no space on either side in a range of numbers.
Raw output
{"message": "[OpenSearch.Range] Use an en dash (--) with no space on either side in a range of numbers.", "location": {"path": "_sql-and-ppl/ppl/commands/ad.md", "range": {"start": {"line": 116, "column": 18}}}, "severity": "ERROR"}


## Example 3: Detecting events in New York City taxi ridership non-time-series data

Check failure on line 119 in _sql-and-ppl/ppl/commands/ad.md

View workflow job for this annotation

GitHub Actions / vale

[vale] _sql-and-ppl/ppl/commands/ad.md#L119

[OpenSearch.HeadingCapitalization] 'Example 3: Detecting events in New York City taxi ridership non-time-series data' is a heading and should be in sentence case.
Raw output
{"message": "[OpenSearch.HeadingCapitalization] 'Example 3: Detecting events in New York City taxi ridership non-time-series data' is a heading and should be in sentence case.", "location": {"path": "_sql-and-ppl/ppl/commands/ad.md", "range": {"start": {"line": 119, "column": 4}}}, "severity": "ERROR"}

This example trains an RCF model and uses it to detect anomalies in non-time-series ridership data:

```sql
source=nyc_taxi
| fields value
| AD
| where value=10844.0
```
{% include copy.html %}

The query returns the following results:

| value | score | anomalous |

Check failure on line 133 in _sql-and-ppl/ppl/commands/ad.md

View workflow job for this annotation

GitHub Actions / vale

[vale] _sql-and-ppl/ppl/commands/ad.md#L133

[OpenSearch.TableHeadings] 'value' is a table heading and should be in sentence case.
Raw output
{"message": "[OpenSearch.TableHeadings] 'value' is a table heading and should be in sentence case.", "location": {"path": "_sql-and-ppl/ppl/commands/ad.md", "range": {"start": {"line": 133, "column": 3}}}, "severity": "ERROR"}

Check failure on line 133 in _sql-and-ppl/ppl/commands/ad.md

View workflow job for this annotation

GitHub Actions / vale

[vale] _sql-and-ppl/ppl/commands/ad.md#L133

[OpenSearch.TableHeadings] 'score' is a table heading and should be in sentence case.
Raw output
{"message": "[OpenSearch.TableHeadings] 'score' is a table heading and should be in sentence case.", "location": {"path": "_sql-and-ppl/ppl/commands/ad.md", "range": {"start": {"line": 133, "column": 11}}}, "severity": "ERROR"}

Check failure on line 133 in _sql-and-ppl/ppl/commands/ad.md

View workflow job for this annotation

GitHub Actions / vale

[vale] _sql-and-ppl/ppl/commands/ad.md#L133

[OpenSearch.TableHeadings] 'anomalous' is a table heading and should be in sentence case.
Raw output
{"message": "[OpenSearch.TableHeadings] 'anomalous' is a table heading and should be in sentence case.", "location": {"path": "_sql-and-ppl/ppl/commands/ad.md", "range": {"start": {"line": 133, "column": 19}}}, "severity": "ERROR"}
| --- | --- | --- |
| 10844.0 | 0.0 | False |


## Example 4: Detecting events in New York City taxi ridership non-time-series data by category

Check failure on line 138 in _sql-and-ppl/ppl/commands/ad.md

View workflow job for this annotation

GitHub Actions / vale

[vale] _sql-and-ppl/ppl/commands/ad.md#L138

[OpenSearch.HeadingCapitalization] 'Example 4: Detecting events in New York City taxi ridership non-time-series data by category' is a heading and should be in sentence case.
Raw output
{"message": "[OpenSearch.HeadingCapitalization] 'Example 4: Detecting events in New York City taxi ridership non-time-series data by category' is a heading and should be in sentence case.", "location": {"path": "_sql-and-ppl/ppl/commands/ad.md", "range": {"start": {"line": 138, "column": 4}}}, "severity": "ERROR"}

This example trains an RCF model and uses it to detect anomalies in non-time-series ridership data across multiple category values:

```sql
source=nyc_taxi
| fields category, value
| AD category_field='category'
| where value=10844.0 or value=6526.0
```
{% include copy.html %}

The query returns the following results:

| category | value | score | anomalous |

Check failure on line 152 in _sql-and-ppl/ppl/commands/ad.md

View workflow job for this annotation

GitHub Actions / vale

[vale] _sql-and-ppl/ppl/commands/ad.md#L152

[OpenSearch.TableHeadings] 'value' is a table heading and should be in sentence case.
Raw output
{"message": "[OpenSearch.TableHeadings] 'value' is a table heading and should be in sentence case.", "location": {"path": "_sql-and-ppl/ppl/commands/ad.md", "range": {"start": {"line": 152, "column": 14}}}, "severity": "ERROR"}

Check failure on line 152 in _sql-and-ppl/ppl/commands/ad.md

View workflow job for this annotation

GitHub Actions / vale

[vale] _sql-and-ppl/ppl/commands/ad.md#L152

[OpenSearch.TableHeadings] 'score' is a table heading and should be in sentence case.
Raw output
{"message": "[OpenSearch.TableHeadings] 'score' is a table heading and should be in sentence case.", "location": {"path": "_sql-and-ppl/ppl/commands/ad.md", "range": {"start": {"line": 152, "column": 22}}}, "severity": "ERROR"}

Check failure on line 152 in _sql-and-ppl/ppl/commands/ad.md

View workflow job for this annotation

GitHub Actions / vale

[vale] _sql-and-ppl/ppl/commands/ad.md#L152

[OpenSearch.TableHeadings] 'anomalous' is a table heading and should be in sentence case.
Raw output
{"message": "[OpenSearch.TableHeadings] 'anomalous' is a table heading and should be in sentence case.", "location": {"path": "_sql-and-ppl/ppl/commands/ad.md", "range": {"start": {"line": 152, "column": 30}}}, "severity": "ERROR"}
| --- | --- | --- | --- |
| night | 10844.0 | 0.0 | False |
| day | 6526.0 | 0.0 | False |


94 changes: 94 additions & 0 deletions _sql-and-ppl/ppl/commands/addcoltotals.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
---
layout: default
title: addcoltotals
parent: Commands
grand_parent: PPL
nav_order: 2
---

# addcoltotals

Check failure on line 9 in _sql-and-ppl/ppl/commands/addcoltotals.md

View workflow job for this annotation

GitHub Actions / vale

[vale] _sql-and-ppl/ppl/commands/addcoltotals.md#L9

[OpenSearch.Spelling] Error: addcoltotals. If you are referencing a setting, variable, format, function, or repository, surround it with tic marks.
Raw output
{"message": "[OpenSearch.Spelling] Error: addcoltotals. If you are referencing a setting, variable, format, function, or repository, surround it with tic marks.", "location": {"path": "_sql-and-ppl/ppl/commands/addcoltotals.md", "range": {"start": {"line": 9, "column": 3}}}, "severity": "ERROR"}

Check failure on line 9 in _sql-and-ppl/ppl/commands/addcoltotals.md

View workflow job for this annotation

GitHub Actions / vale

[vale] _sql-and-ppl/ppl/commands/addcoltotals.md#L9

[OpenSearch.HeadingCapitalization] 'addcoltotals' is a heading and should be in sentence case.
Raw output
{"message": "[OpenSearch.HeadingCapitalization] 'addcoltotals' is a heading and should be in sentence case.", "location": {"path": "_sql-and-ppl/ppl/commands/addcoltotals.md", "range": {"start": {"line": 9, "column": 3}}}, "severity": "ERROR"}

The `addcoltotals` command computes the sum of each column and adds a summary row showing the total for each column. This command is equivalent to using `addtotals` with `row=false` and `col=true`, making it useful for creating summary reports with column totals.

The command only processes numeric fields (integers, floats, doubles). Non-numeric fields are ignored regardless of whether they are explicitly specified in the field list.


## Syntax

The `addcoltotals` command has the following syntax:

```sql
addcoltotals [field-list] [label=<string>] [labelfield=<field>]
```

## Parameters

The `addcoltotals` command supports the following parameters.

| Parameter | Required/Optional | Description |
| --- | --- | --- |
| `<field-list>` | Optional | A comma-separated list of numeric fields to add. By default, all numeric fields are added. |
| `labelfield` | Optional | The field in which the label is placed. If the field does not exist, it is created and the label is shown in the summary row (last row) of the new field. |
| `label` | Optional | The text that appears in the summary row (last row) to identify the computed totals. When used with `labelfield`, this text is placed in the specified field in the summary row. Default is `Total`. |

### Example 1: Basic example

The following query places the label in an existing field:

```sql
source=accounts
| fields firstname, balance
| head 3
| addcoltotals labelfield='firstname'
```
{% include copy.html %}

The query returns the following results:

| firstname | balance |

Check failure on line 48 in _sql-and-ppl/ppl/commands/addcoltotals.md

View workflow job for this annotation

GitHub Actions / vale

[vale] _sql-and-ppl/ppl/commands/addcoltotals.md#L48

[OpenSearch.TableHeadings] 'firstname' is a table heading and should be in sentence case.
Raw output
{"message": "[OpenSearch.TableHeadings] 'firstname' is a table heading and should be in sentence case.", "location": {"path": "_sql-and-ppl/ppl/commands/addcoltotals.md", "range": {"start": {"line": 48, "column": 3}}}, "severity": "ERROR"}

Check failure on line 48 in _sql-and-ppl/ppl/commands/addcoltotals.md

View workflow job for this annotation

GitHub Actions / vale

[vale] _sql-and-ppl/ppl/commands/addcoltotals.md#L48

[OpenSearch.Spelling] Error: firstname. If you are referencing a setting, variable, format, function, or repository, surround it with tic marks.
Raw output
{"message": "[OpenSearch.Spelling] Error: firstname. If you are referencing a setting, variable, format, function, or repository, surround it with tic marks.", "location": {"path": "_sql-and-ppl/ppl/commands/addcoltotals.md", "range": {"start": {"line": 48, "column": 3}}}, "severity": "ERROR"}

Check failure on line 48 in _sql-and-ppl/ppl/commands/addcoltotals.md

View workflow job for this annotation

GitHub Actions / vale

[vale] _sql-and-ppl/ppl/commands/addcoltotals.md#L48

[OpenSearch.TableHeadings] 'balance' is a table heading and should be in sentence case.
Raw output
{"message": "[OpenSearch.TableHeadings] 'balance' is a table heading and should be in sentence case.", "location": {"path": "_sql-and-ppl/ppl/commands/addcoltotals.md", "range": {"start": {"line": 48, "column": 15}}}, "severity": "ERROR"}
| --- | --- |
| Amber | 39225 |
| Hattie | 5686 |
| Nanette | 32838 |
| Total | 77749 |

## Example 2: Adding column totals with a custom summary label

The following query adds totals after a `stats` command where the final summary event label is `Sum`. It also creates a new field specified by `labelfield` because this field does not exist in the data:

```sql
source=accounts
| stats count() by gender
| addcoltotals `count()` label='Sum' labelfield='Total'
```
{% include copy.html %}

The query returns the following results:

| count() | gender | Total |

Check failure on line 68 in _sql-and-ppl/ppl/commands/addcoltotals.md

View workflow job for this annotation

GitHub Actions / vale

[vale] _sql-and-ppl/ppl/commands/addcoltotals.md#L68

[OpenSearch.TableHeadings] 'count()' is a table heading and should be in sentence case.
Raw output
{"message": "[OpenSearch.TableHeadings] 'count()' is a table heading and should be in sentence case.", "location": {"path": "_sql-and-ppl/ppl/commands/addcoltotals.md", "range": {"start": {"line": 68, "column": 3}}}, "severity": "ERROR"}

Check failure on line 68 in _sql-and-ppl/ppl/commands/addcoltotals.md

View workflow job for this annotation

GitHub Actions / vale

[vale] _sql-and-ppl/ppl/commands/addcoltotals.md#L68

[OpenSearch.TableHeadings] 'gender' is a table heading and should be in sentence case.
Raw output
{"message": "[OpenSearch.TableHeadings] 'gender' is a table heading and should be in sentence case.", "location": {"path": "_sql-and-ppl/ppl/commands/addcoltotals.md", "range": {"start": {"line": 68, "column": 13}}}, "severity": "ERROR"}
| --- | --- | --- |
| 1 | F | null |
| 3 | M | null |
| 4 | null | Sum |

## Example 3: Using all options

The following query uses the `addcoltotals` command with all options set:

```sql
source=accounts
| where age > 30
| stats avg(balance) as avg_balance, count() as count by state
| head 3
| addcoltotals avg_balance, count label='Sum' labelfield='Column Total'
```
{% include copy.html %}

The query returns the following results:

| avg_balance | count | state | Column Total |

Check failure on line 89 in _sql-and-ppl/ppl/commands/addcoltotals.md

View workflow job for this annotation

GitHub Actions / vale

[vale] _sql-and-ppl/ppl/commands/addcoltotals.md#L89

[OpenSearch.TableHeadings] 'count' is a table heading and should be in sentence case.
Raw output
{"message": "[OpenSearch.TableHeadings] 'count' is a table heading and should be in sentence case.", "location": {"path": "_sql-and-ppl/ppl/commands/addcoltotals.md", "range": {"start": {"line": 89, "column": 17}}}, "severity": "ERROR"}

Check failure on line 89 in _sql-and-ppl/ppl/commands/addcoltotals.md

View workflow job for this annotation

GitHub Actions / vale

[vale] _sql-and-ppl/ppl/commands/addcoltotals.md#L89

[OpenSearch.TableHeadings] 'state' is a table heading and should be in sentence case.
Raw output
{"message": "[OpenSearch.TableHeadings] 'state' is a table heading and should be in sentence case.", "location": {"path": "_sql-and-ppl/ppl/commands/addcoltotals.md", "range": {"start": {"line": 89, "column": 25}}}, "severity": "ERROR"}

Check failure on line 89 in _sql-and-ppl/ppl/commands/addcoltotals.md

View workflow job for this annotation

GitHub Actions / vale

[vale] _sql-and-ppl/ppl/commands/addcoltotals.md#L89

[OpenSearch.TableHeadings] 'Column Total' is a table heading and should be in sentence case.
Raw output
{"message": "[OpenSearch.TableHeadings] 'Column Total' is a table heading and should be in sentence case.", "location": {"path": "_sql-and-ppl/ppl/commands/addcoltotals.md", "range": {"start": {"line": 89, "column": 33}}}, "severity": "ERROR"}
| --- | --- | --- | --- |
| 39225.0 | 1 | IL | null |
| 4180.0 | 1 | MD | null |
| 5686.0 | 1 | TN | null |
| 49091.0 | 3 | null | Sum |
120 changes: 120 additions & 0 deletions _sql-and-ppl/ppl/commands/addtotals.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
---
layout: default
title: addtotals
parent: Commands
grand_parent: PPL
nav_order: 3
---

# addtotals

Check failure on line 9 in _sql-and-ppl/ppl/commands/addtotals.md

View workflow job for this annotation

GitHub Actions / vale

[vale] _sql-and-ppl/ppl/commands/addtotals.md#L9

[OpenSearch.Spelling] Error: addtotals. If you are referencing a setting, variable, format, function, or repository, surround it with tic marks.
Raw output
{"message": "[OpenSearch.Spelling] Error: addtotals. If you are referencing a setting, variable, format, function, or repository, surround it with tic marks.", "location": {"path": "_sql-and-ppl/ppl/commands/addtotals.md", "range": {"start": {"line": 9, "column": 3}}}, "severity": "ERROR"}

Check failure on line 9 in _sql-and-ppl/ppl/commands/addtotals.md

View workflow job for this annotation

GitHub Actions / vale

[vale] _sql-and-ppl/ppl/commands/addtotals.md#L9

[OpenSearch.HeadingCapitalization] 'addtotals' is a heading and should be in sentence case.
Raw output
{"message": "[OpenSearch.HeadingCapitalization] 'addtotals' is a heading and should be in sentence case.", "location": {"path": "_sql-and-ppl/ppl/commands/addtotals.md", "range": {"start": {"line": 9, "column": 3}}}, "severity": "ERROR"}

The `addtotals` command computes the sum of numeric fields and can create both column totals (summary row) and row totals (new field). This command is useful for creating summary reports with subtotals or grand totals.

The command only processes numeric fields (integers, floats, doubles). Non-numeric fields are ignored regardless of whether they are explicitly specified in the field list.


## Syntax

The `addtotals` command has the following syntax:

```sql
addtotals [field-list] [label=<string>] [labelfield=<field>] [row=<boolean>] [col=<boolean>] [fieldname=<field>]
```

## Parameters

The `addtotals` command supports the following parameters.

| Parameter | Required/Optional | Description |
| --- | --- | --- |
| `<field-list>` | Optional | A comma-separated list of numeric fields to add. By default, all numeric fields are added. |
| `row` | Optional | Calculates the total of each row and adds a new field to store the row total. Default is `true`. |
| `col` | Optional | Calculates the total of each column and adds a summary event at the end with the column totals. Default is `false`. |
| `labelfield` | Optional | The field in which the label is placed. If the field does not exist, it is created and the label is shown in the summary row (last row) of the new field. Applicable when `col=true`. |
| `label` | Optional | The text that appears in the summary row (last row) to identify the computed totals. When used with `labelfield`, this text is placed in the specified field in the summary row. Default is `Total`. Applicable when `col=true`. This parameter has no effect when the `labelfield` and `fieldname` parameters specify the same field name. |
| `fieldname` | Optional | The field used to store row totals. Applicable when `row=true`. |

## Example 1: Basic example

The following query places the label in an existing field:

```sql
source=accounts
| head 3
| fields firstname, balance
| addtotals col=true labelfield='firstname' label='Total'
```
{% include copy.html %}

The query returns the following results:

| firstname | balance | Total |

Check failure on line 51 in _sql-and-ppl/ppl/commands/addtotals.md

View workflow job for this annotation

GitHub Actions / vale

[vale] _sql-and-ppl/ppl/commands/addtotals.md#L51

[OpenSearch.TableHeadings] 'firstname' is a table heading and should be in sentence case.
Raw output
{"message": "[OpenSearch.TableHeadings] 'firstname' is a table heading and should be in sentence case.", "location": {"path": "_sql-and-ppl/ppl/commands/addtotals.md", "range": {"start": {"line": 51, "column": 3}}}, "severity": "ERROR"}

Check failure on line 51 in _sql-and-ppl/ppl/commands/addtotals.md

View workflow job for this annotation

GitHub Actions / vale

[vale] _sql-and-ppl/ppl/commands/addtotals.md#L51

[OpenSearch.Spelling] Error: firstname. If you are referencing a setting, variable, format, function, or repository, surround it with tic marks.
Raw output
{"message": "[OpenSearch.Spelling] Error: firstname. If you are referencing a setting, variable, format, function, or repository, surround it with tic marks.", "location": {"path": "_sql-and-ppl/ppl/commands/addtotals.md", "range": {"start": {"line": 51, "column": 3}}}, "severity": "ERROR"}

Check failure on line 51 in _sql-and-ppl/ppl/commands/addtotals.md

View workflow job for this annotation

GitHub Actions / vale

[vale] _sql-and-ppl/ppl/commands/addtotals.md#L51

[OpenSearch.TableHeadings] 'balance' is a table heading and should be in sentence case.
Raw output
{"message": "[OpenSearch.TableHeadings] 'balance' is a table heading and should be in sentence case.", "location": {"path": "_sql-and-ppl/ppl/commands/addtotals.md", "range": {"start": {"line": 51, "column": 15}}}, "severity": "ERROR"}
| --- | --- | --- |
| Amber | 39225 | 39225 |
| Hattie | 5686 | 5686 |
| Nanette | 32838 | 32838 |
| Total | 77749 | null |

## Example 2: Adding column totals with a custom summary label

The following query adds totals after a `stats` command, with the final summary event labeled `Sum`. It also creates a new field specified by `labelfield` because the field does not exist in the data:


```sql
source=accounts
| fields account_number, firstname , balance , age
| addtotals col=true row=false label='Sum' labelfield='Total'
```
{% include copy.html %}

The query returns the following results:

| account_number | firstname | balance | age | Total |

Check failure on line 72 in _sql-and-ppl/ppl/commands/addtotals.md

View workflow job for this annotation

GitHub Actions / vale

[vale] _sql-and-ppl/ppl/commands/addtotals.md#L72

[OpenSearch.TableHeadings] 'firstname' is a table heading and should be in sentence case.
Raw output
{"message": "[OpenSearch.TableHeadings] 'firstname' is a table heading and should be in sentence case.", "location": {"path": "_sql-and-ppl/ppl/commands/addtotals.md", "range": {"start": {"line": 72, "column": 20}}}, "severity": "ERROR"}

Check failure on line 72 in _sql-and-ppl/ppl/commands/addtotals.md

View workflow job for this annotation

GitHub Actions / vale

[vale] _sql-and-ppl/ppl/commands/addtotals.md#L72

[OpenSearch.Spelling] Error: firstname. If you are referencing a setting, variable, format, function, or repository, surround it with tic marks.
Raw output
{"message": "[OpenSearch.Spelling] Error: firstname. If you are referencing a setting, variable, format, function, or repository, surround it with tic marks.", "location": {"path": "_sql-and-ppl/ppl/commands/addtotals.md", "range": {"start": {"line": 72, "column": 20}}}, "severity": "ERROR"}

Check failure on line 72 in _sql-and-ppl/ppl/commands/addtotals.md

View workflow job for this annotation

GitHub Actions / vale

[vale] _sql-and-ppl/ppl/commands/addtotals.md#L72

[OpenSearch.TableHeadings] 'balance' is a table heading and should be in sentence case.
Raw output
{"message": "[OpenSearch.TableHeadings] 'balance' is a table heading and should be in sentence case.", "location": {"path": "_sql-and-ppl/ppl/commands/addtotals.md", "range": {"start": {"line": 72, "column": 32}}}, "severity": "ERROR"}

Check failure on line 72 in _sql-and-ppl/ppl/commands/addtotals.md

View workflow job for this annotation

GitHub Actions / vale

[vale] _sql-and-ppl/ppl/commands/addtotals.md#L72

[OpenSearch.TableHeadings] 'age' is a table heading and should be in sentence case.
Raw output
{"message": "[OpenSearch.TableHeadings] 'age' is a table heading and should be in sentence case.", "location": {"path": "_sql-and-ppl/ppl/commands/addtotals.md", "range": {"start": {"line": 72, "column": 42}}}, "severity": "ERROR"}
| --- | --- | --- | --- | --- |
| 1 | Amber | 39225 | 32 | null |
| 6 | Hattie | 5686 | 36 | null |
| 13 | Nanette | 32838 | 28 | null |
| 18 | Dale | 4180 | 33 | null |
| 38 | null | 81929 | 129 | Sum |

If you set `row=true` in the preceding example, both row totals and column totals try to use the same field name (`Total`), creating a conflict. When this happens, the summary row label displays as `null` instead of `Sum` because the field becomes numeric (for row totals) and cannot display string values:


```sql
source=accounts
| fields account_number, firstname , balance , age

Check failure on line 85 in _sql-and-ppl/ppl/commands/addtotals.md

View workflow job for this annotation

GitHub Actions / vale

[vale] _sql-and-ppl/ppl/commands/addtotals.md#L85

[OpenSearch.TableHeadings] 'count' is a table heading and should be in sentence case.
Raw output
{"message": "[OpenSearch.TableHeadings] 'count' is a table heading and should be in sentence case.", "location": {"path": "_sql-and-ppl/ppl/commands/addtotals.md", "range": {"start": {"line": 85, "column": 14}}}, "severity": "ERROR"}
| addtotals col=true row=true label='Sum' labelfield='Total'
```
{% include copy.html %}

The query returns the following results:

| account_number | firstname | balance | age | Total |

Check failure on line 92 in _sql-and-ppl/ppl/commands/addtotals.md

View workflow job for this annotation

GitHub Actions / vale

[vale] _sql-and-ppl/ppl/commands/addtotals.md#L92

[OpenSearch.Spelling] Error: firstname. If you are referencing a setting, variable, format, function, or repository, surround it with tic marks.
Raw output
{"message": "[OpenSearch.Spelling] Error: firstname. If you are referencing a setting, variable, format, function, or repository, surround it with tic marks.", "location": {"path": "_sql-and-ppl/ppl/commands/addtotals.md", "range": {"start": {"line": 92, "column": 20}}}, "severity": "ERROR"}

Check failure on line 92 in _sql-and-ppl/ppl/commands/addtotals.md

View workflow job for this annotation

GitHub Actions / vale

[vale] _sql-and-ppl/ppl/commands/addtotals.md#L92

[OpenSearch.TableHeadings] 'firstname' is a table heading and should be in sentence case.
Raw output
{"message": "[OpenSearch.TableHeadings] 'firstname' is a table heading and should be in sentence case.", "location": {"path": "_sql-and-ppl/ppl/commands/addtotals.md", "range": {"start": {"line": 92, "column": 20}}}, "severity": "ERROR"}

Check failure on line 92 in _sql-and-ppl/ppl/commands/addtotals.md

View workflow job for this annotation

GitHub Actions / vale

[vale] _sql-and-ppl/ppl/commands/addtotals.md#L92

[OpenSearch.TableHeadings] 'balance' is a table heading and should be in sentence case.
Raw output
{"message": "[OpenSearch.TableHeadings] 'balance' is a table heading and should be in sentence case.", "location": {"path": "_sql-and-ppl/ppl/commands/addtotals.md", "range": {"start": {"line": 92, "column": 32}}}, "severity": "ERROR"}

Check failure on line 92 in _sql-and-ppl/ppl/commands/addtotals.md

View workflow job for this annotation

GitHub Actions / vale

[vale] _sql-and-ppl/ppl/commands/addtotals.md#L92

[OpenSearch.TableHeadings] 'age' is a table heading and should be in sentence case.
Raw output
{"message": "[OpenSearch.TableHeadings] 'age' is a table heading and should be in sentence case.", "location": {"path": "_sql-and-ppl/ppl/commands/addtotals.md", "range": {"start": {"line": 92, "column": 42}}}, "severity": "ERROR"}
| --- | --- | --- | --- | --- |
| 1 | Amber | 39225 | 32 | 39258 |
| 6 | Hattie | 5686 | 36 | 5728 |
| 13 | Nanette | 32838 | 28 | 32879 |
| 18 | Dale | 4180 | 33 | 4231 |
| 38 | null | 81929 | 129 | null |

## Example 3: Using all options

The following query uses the `addtotals` command with all options set:

```sql
source=accounts
| where age > 30
| stats avg(balance) as avg_balance, count() as count by state
| head 3
| addtotals avg_balance, count row=true col=true fieldname='Row Total' label='Sum' labelfield='Column Total'
```
{% include copy.html %}

The query returns the following results:

| avg_balance | count | state | Row Total | Column Total |

Check failure on line 115 in _sql-and-ppl/ppl/commands/addtotals.md

View workflow job for this annotation

GitHub Actions / vale

[vale] _sql-and-ppl/ppl/commands/addtotals.md#L115

[OpenSearch.TableHeadings] 'state' is a table heading and should be in sentence case.
Raw output
{"message": "[OpenSearch.TableHeadings] 'state' is a table heading and should be in sentence case.", "location": {"path": "_sql-and-ppl/ppl/commands/addtotals.md", "range": {"start": {"line": 115, "column": 25}}}, "severity": "ERROR"}

Check failure on line 115 in _sql-and-ppl/ppl/commands/addtotals.md

View workflow job for this annotation

GitHub Actions / vale

[vale] _sql-and-ppl/ppl/commands/addtotals.md#L115

[OpenSearch.TableHeadings] 'Row Total' is a table heading and should be in sentence case.
Raw output
{"message": "[OpenSearch.TableHeadings] 'Row Total' is a table heading and should be in sentence case.", "location": {"path": "_sql-and-ppl/ppl/commands/addtotals.md", "range": {"start": {"line": 115, "column": 33}}}, "severity": "ERROR"}

Check failure on line 115 in _sql-and-ppl/ppl/commands/addtotals.md

View workflow job for this annotation

GitHub Actions / vale

[vale] _sql-and-ppl/ppl/commands/addtotals.md#L115

[OpenSearch.TableHeadings] 'Column Total' is a table heading and should be in sentence case.
Raw output
{"message": "[OpenSearch.TableHeadings] 'Column Total' is a table heading and should be in sentence case.", "location": {"path": "_sql-and-ppl/ppl/commands/addtotals.md", "range": {"start": {"line": 115, "column": 45}}}, "severity": "ERROR"}
| --- | --- | --- | --- | --- |
| 39225.0 | 1 | IL | 39226.0 | null |
| 4180.0 | 1 | MD | 4181.0 | null |
| 5686.0 | 1 | TN | 5687.0 | null |
| 49091.0 | 3 | null | null | Sum |
Loading