addtotals

addtotals [options(...)]?, [column: storable]*

Adds a column that totals the numeric columns in each row.

addtotals compiles only when the enable_opal_totals feature flag is on for the customer. The same flag gates addcoltotals.

By default the new column is named Total and sums every int64, float64, and numeric column. options(fieldname: "name") chooses a different name. Timestamp, duration, string, bool, and object columns are left out of that default sum, including the valid-from and valid-to columns. A null cell counts as zero. The destination cannot be the valid-from, valid-to, or a primary key column. Any other existing column with the destination name is overwritten, and that column is left out of the default sum and out of quoted patterns so its previous value is not added into itself. Name that column in the argument list to include the previous value.

Pass column names or quoted patterns ("amount*", "*size*") to choose the columns. A pattern uses * and ?; every other character, including / and [, is matched literally. Patterns and the default column set skip hidden and internal columns. A pattern that matches nothing is an error. A duration column is included when it is named or matched by a pattern, and a row total of only duration columns has type duration. A timestamp column cannot be totaled. Mixing a duration column with an int or float column is an error.

options(col: true) also appends one summary row, as addcoltotals does, including options(labelfield: column, label: "Total"). options(row: false) turns the new column off. Setting both row and col to false is an error.

Total on the summary row

With options(col: true), each data row still gets a Total, and a summary row is appended. Total still sums only int64, float64, and numeric columns. The summary row sums those columns and also sums each duration column.

The Total value on the summary row is the column total of the Total column: the sum of the per-row Total values. It is not a row total of the summary row. A duration total therefore appears in its own column on the summary row and is not added into Total.

For columns count (int64) and latency (duration), Total on each data row equals count. On the summary row, count is the sum of count, latency is the sum of latency, and Total is the sum of the data rows' Total values.

The summary row is sorted last. A later sort or pick_col keeps it last. A later limit, or the row limit applied when results are displayed, can drop it when there are more data rows than the limit. Column totals read every input row that reaches the verb.

Options

OptionTypeMeaning
fieldnamestringName of the row-total column. Defaults to Total.
rowboolWhen true, add the row-total column. Defaults to true.
colboolWhen true, append a summary row of column totals. Defaults to false.
labelfieldstringColumn that receives the summary label. Used when col is true.
labelstringText written into labelfield on the summary row. Defaults to Total.

Categories

Accelerable

addtotals is sometimes accelerable, depending on options used. A dataset that only uses accelerable verbs can be accelerated, making
queries on the dataset respond faster.

Examples

addtotals "price"

Adds a Total column that sums the named price column on each row.

addtotals

With no column list, each row total sums every int64, float64, and numeric column.

addtotals "a", "b"

An existing Total column is overwritten with the new row totals. Those previous cells are not added into the new values unless you name the column without quotes (addtotals a, b, Total). A quoted "Total" is a pattern and is skipped, so that form fails to compile.

addtotals options(col: true), "price"
limit 2

A later limit can drop the summary row when there are more data rows than the limit. Here the limit keeps two rows, so the summary row and the third data row are cut. Column totals still cover every row that reached addtotals.

addtotals options(col: true), "price"
sort desc(price)

A later sort reorders the data rows and keeps the summary row last.

addtotals options(fieldname: "line_total"), "price"

Renames the row-total column from the default Total to line_total.

addtotals "p*"

A quoted pattern selects columns by name. "p*" matches names that start with p.

addtotals "sold", "returned"

A null cell in a summed column counts as zero, so a row with a null returned value still gets a row total.

addtotals "queue_wait", "handle_time"

Naming duration columns includes them in the row total. Total has type duration and is the sum of queue_wait and handle_time.

addtotals options(col: true), "price"

options(col: true) still adds a per-row Total and also appends one summary row. The Total on that row is the sum of the per-row totals.

addtotals options(row: false, col: true, labelfield: "title", label: "Grand Total"), "price"

Turns the row-total column off and keeps only a summary row, writing a custom label into title.

addtotals options(col: true, labelfield: "products", label: "Quarterly Totals", fieldname: "Product Totals"), "QTR1", "QTR2", "QTR3", "QTR4"

Combines a renamed row-total column with a labeled summary row. The corner cell is the sum of the per-row totals, not a new sum of the summary row.

addtotals options(col: true)

The default row total skips duration columns. The summary row still totals duration, and Total there is the sum of the per-row Total values.


Did this page help you?