CREATE TEMP TABLE my_data AS
SELECT DISTINCT ON (species, island, sex) * FROM ggsql:penguins
WHERE bill_len IS NOT NULL AND sex IS NOT NULL
TABULATE * FROM my_data
SUMMARISE mean OF bill_len, bill_dep| mean |
|---|
ggsql is still in beta and many parts are being worked out. Read the beta-release announcement
Values can be summarised and displayed in dedicated rows. This is helpful to display observations alongside statistics in the table. SUMMARISE can be repeated to compute multiple summaries.
Also accepts the spelling SUMMARIZE.
The scope directly following the SUMMARISE clause determines which rows the operation aggregates over. Setting the scope to GROUP will compute the summary for each group determined by FACET clauses separately. The GRAND scope will compute the summary for all rows. Without spelling out a scope, the default will be GROUP. When no groups are declared by FACET and the scope is set to GROUP, all rows are treated as belonging to the same group. The difference between a FACET-less GROUP scope and a GRAND scope is largely a difference in style and label spanning. A query result with no rows cannot be summarised: SUMMARISE errors on an empty table.
This part of the clause can be an identifier or a string representing an expression. The identifiers are the same ones used in stat aggregation in plots. When using an identifier, one of the following operations are available:
| Name | Description | Numeric | Date(time) | Other |
|---|---|---|---|---|
count |
Number of rows | ✓ | ✓ | ✓ |
n_distinct |
Number of distinct values | ✓ | ✓ | ✓ |
n_missing |
Number of nulls | ✓ | ✓ | ✓ |
sum |
Sum | ✓ | ||
prod |
Product | ✓ | ||
min |
Minimum | ✓ | ✓ | |
max |
Maximum | ✓ | ✓ | |
range |
max − min | ✓ | ✓ | |
mid |
(min + max) / 2 | ✓ | ✓ | |
mean |
Arithmetic mean | ✓ | ✓ | |
geomean |
Geometric mean | ✓ | ||
harmean |
Harmonic mean | ✓ | ||
rms |
Root-mean-square | ✓ | ||
median |
Median | ✓ | ✓ | |
sdev |
Standard deviation (population, ÷ n) | ✓ | ✓ | |
se |
Standard error (population, ÷ n) | ✓ | ✓ | |
var |
Variance (population, ÷ n) | ✓ | ||
iqr |
Interquartile range | ✓ | ✓ | |
p05 |
5th percentile | ✓ | ✓ | |
p10 |
10th percentile | ✓ | ✓ | |
p25 |
25th percentile | ✓ | ✓ | |
p50 |
50th percentile (median) | ✓ | ✓ | |
p75 |
75th percentile | ✓ | ✓ | |
p90 |
90th percentile | ✓ | ✓ | |
p95 |
95th percentile | ✓ | ✓ |
When using a string as an expression, you can use any of the identifiers in curly braces. For example '{mean}' will display the mean via expression. The expressions can be used to display multiple metrics in a single cell, e.g. '{median} ({p25}, {p75})' will display the median and interquartile range in one cell. Inside the brackets, you can use limited arithmetic. The operands include the operation identifiers listed above (like mean, sdev), numeric literals (2, 0.5) and scientific notation (1e6). The +, - unary operators and +, -, * and / binary operators and (/) parentheses for grouping. For example you can use '({mean - 1.96 * se}, {mean + 1.96 * se})' to display a 95% confidence interval (assuming normal distribution) in parentheses separated by a comma. If any placeholder in a string expression comes out null (see missing below), the whole cell comes out null.
For the naked, non-string identifiers the formatting of the cell values is applied externally via FORMAT clauses. The string expressions can be self-formatting via the same syntax as scale breaks. A string-expression cell ignores a FORMAT wildcard label, but direct labels still apply — including null => … to replace a null result. All summary metrics are assumed to be numeric. As such, the :num formatter is implied and does not need to be written out in full. The expression '{mean:%.2f}' will display the mean with two decimal points in printf-style. Other formatters can be used, e.g. {max:bytes} to display base-1024 size. When using arithmetic, the formatting is placed at the end and is applied to the result so that '{mean + sdev:%.2f}' rounds the expression to two decimals.
Date and timestamp columns accept the order statistics (min, max, mid, mean, median and the percentiles), returning a date or timestamp, and range, sdev, se and iqr, returning a duration. A date or timestamp renders as ISO text or via :time ('{min:time %d %b %Y}'); in arithmetic it becomes a plain number: days for dates, seconds for timestamps — so '{max-min:duration d}' renders a date range as a duration. A date-valued mean or mid rounds to the nearest whole day.
The SUMMARISE clause requires a comma-separated list of columns to aggregate. The columns must be part of the TABULATE selection. The ‘counting’ operations listed above can be applied to columns of any type, the order and dispersion statistics also accept date and timestamp columns, and the remaining operations require numeric columns.
The SETTING sub-clause is typical with key-value pairs. It currently takes the following parameters:
place: the location of the summary rows relative to the body rows they summarise. Can be 'top' to place the summary rows at the top of each group or table, or 'bottom' (default) to place the summary rows after the body rows.rowlabel: A string for the label of the summary row. The default null falls back to printing the operation, where string expression operations are displayed without formatting details.missing: A string option for how to treat null values in the table. The default 'propagate' will list any non-counting operation as null, while the tallies count and n_missing include null-rows — note that n_distinct is not a tally and comes out null when the column contains any. Can be set to 'drop' to explicitly ignore null values in aggregates and ignore these rows in counting operations.blank: A string displayed in cells of the summary row whose column are not summarised. The default is an em dash ('—'); set it to '' for empty cells.A summary without groups will summarise all rows.
| species | island | bill_len | bill_dep | flipper_len | body_mass | sex | year | |
|---|---|---|---|---|---|---|---|---|
| Adelie | Dream | 39.5 | 16.7 | 178 | 3250 | female | 2007 | |
| Gentoo | Biscoe | 50.0 | 16.3 | 230 | 5700 | male | 2007 | |
| Chinstrap | Dream | 46.5 | 17.9 | 192 | 3500 | female | 2007 | |
| Adelie | Torgersen | 39.5 | 17.4 | 186 | 3800 | female | 2007 | |
| Adelie | Biscoe | 37.8 | 18.3 | 174 | 3400 | female | 2007 | |
| Adelie | Torgersen | 39.1 | 18.7 | 181 | 3750 | male | 2007 | |
| Adelie | Biscoe | 37.7 | 18.7 | 180 | 3600 | male | 2007 | |
| Adelie | Dream | 37.2 | 18.1 | 178 | 3900 | male | 2007 | |
| Gentoo | Biscoe | 46.1 | 13.2 | 211 | 4500 | female | 2007 | |
| Chinstrap | Dream | 50.0 | 19.5 | 196 | 3900 | male | 2007 | |
| mean | — | — | 42.3 | 17.5 | — | — | — | — |
A grand summary will always summarise all rows, and is displayed with a stronger border.
| species | island | bill_len | bill_dep | flipper_len | body_mass | sex | year | |
|---|---|---|---|---|---|---|---|---|
| Adelie | Dream | 39.5 | 16.7 | 178 | 3250 | female | 2007 | |
| Gentoo | Biscoe | 50.0 | 16.3 | 230 | 5700 | male | 2007 | |
| Chinstrap | Dream | 46.5 | 17.9 | 192 | 3500 | female | 2007 | |
| Adelie | Torgersen | 39.5 | 17.4 | 186 | 3800 | female | 2007 | |
| Adelie | Biscoe | 37.8 | 18.3 | 174 | 3400 | female | 2007 | |
| Adelie | Torgersen | 39.1 | 18.7 | 181 | 3750 | male | 2007 | |
| Adelie | Biscoe | 37.7 | 18.7 | 180 | 3600 | male | 2007 | |
| Adelie | Dream | 37.2 | 18.1 | 178 | 3900 | male | 2007 | |
| Gentoo | Biscoe | 46.1 | 13.2 | 211 | 4500 | female | 2007 | |
| Chinstrap | Dream | 50.0 | 19.5 | 196 | 3900 | male | 2007 | |
| mean | — | — | 42.3 | 17.5 | — | — | — | — |
Groups can be specified using FACET.
| species | bill_len | bill_dep | flipper_len | body_mass | sex | year | ||
|---|---|---|---|---|---|---|---|---|
| Biscoe | Gentoo | 50.0 | 16.3 | 230 | 5700 | male | 2007 | |
| Adelie | 37.8 | 18.3 | 174 | 3400 | female | 2007 | ||
| Adelie | 37.7 | 18.7 | 180 | 3600 | male | 2007 | ||
| Gentoo | 46.1 | 13.2 | 211 | 4500 | female | 2007 | ||
| mean | — | 42.9 | 16.6 | — | — | — | — | |
| Dream | Adelie | 39.5 | 16.7 | 178 | 3250 | female | 2007 | |
| Chinstrap | 46.5 | 17.9 | 192 | 3500 | female | 2007 | ||
| Adelie | 37.2 | 18.1 | 178 | 3900 | male | 2007 | ||
| Chinstrap | 50.0 | 19.5 | 196 | 3900 | male | 2007 | ||
| mean | — | 43.3 | 18.0 | — | — | — | — | |
| Torgersen | Adelie | 39.5 | 17.4 | 186 | 3800 | female | 2007 | |
| Adelie | 39.1 | 18.7 | 181 | 3750 | male | 2007 | ||
| mean | — | 39.3 | 18.0 | — | — | — | — |
The labels of summary rows compose with regular body row labels in the stub.
| bill_len | bill_dep | flipper_len | body_mass | sex | year | ||
|---|---|---|---|---|---|---|---|
| Biscoe | Gentoo | 50.0 | 16.3 | 230 | 5700 | male | 2007 |
| Adelie | 37.8 | 18.3 | 174 | 3400 | female | 2007 | |
| Adelie | 37.7 | 18.7 | 180 | 3600 | male | 2007 | |
| Gentoo | 46.1 | 13.2 | 211 | 4500 | female | 2007 | |
| mean | 42.9 | 16.6 | — | — | — | — | |
| Dream | Adelie | 39.5 | 16.7 | 178 | 3250 | female | 2007 |
| Chinstrap | 46.5 | 17.9 | 192 | 3500 | female | 2007 | |
| Adelie | 37.2 | 18.1 | 178 | 3900 | male | 2007 | |
| Chinstrap | 50.0 | 19.5 | 196 | 3900 | male | 2007 | |
| mean | 43.3 | 18.0 | — | — | — | — | |
| Torgersen | Adelie | 39.5 | 17.4 | 186 | 3800 | female | 2007 |
| Adelie | 39.1 | 18.7 | 181 | 3750 | male | 2007 | |
| mean | 39.3 | 18.0 | — | — | — | — |
The display of the label in the stub can be changed using the rowlabel setting.
| island | bill_len | bill_dep | flipper_len | body_mass | sex | year | ||
|---|---|---|---|---|---|---|---|---|
| Adelie | Dream | 39.5 | 16.7 | 178 | 3250 | female | 2007 | |
| Torgersen | 39.5 | 17.4 | 186 | 3800 | female | 2007 | ||
| Biscoe | 37.8 | 18.3 | 174 | 3400 | female | 2007 | ||
| Torgersen | 39.1 | 18.7 | 181 | 3750 | male | 2007 | ||
| Biscoe | 37.7 | 18.7 | 180 | 3600 | male | 2007 | ||
| Dream | 37.2 | 18.1 | 178 | 3900 | male | 2007 | ||
| Average | — | 38.5 | 18.0 | — | — | — | — | |
| Chinstrap | Dream | 46.5 | 17.9 | 192 | 3500 | female | 2007 | |
| Dream | 50.0 | 19.5 | 196 | 3900 | male | 2007 | ||
| Average | — | 48.2 | 18.7 | — | — | — | — | |
| Gentoo | Biscoe | 50.0 | 16.3 | 230 | 5700 | male | 2007 | |
| Biscoe | 46.1 | 13.2 | 211 | 4500 | female | 2007 | ||
| Average | — | 48.0 | 14.8 | — | — | — | — |
The display of non-summary cells can be changed using the blank setting. Using an empty string ('') leaves these cells empty.
| island | bill_len | bill_dep | flipper_len | body_mass | sex | year | ||
|---|---|---|---|---|---|---|---|---|
| Adelie | Dream | 39.5 | 16.7 | 178 | 3250 | female | 2007 | |
| Torgersen | 39.5 | 17.4 | 186 | 3800 | female | 2007 | ||
| Biscoe | 37.8 | 18.3 | 174 | 3400 | female | 2007 | ||
| Torgersen | 39.1 | 18.7 | 181 | 3750 | male | 2007 | ||
| Biscoe | 37.7 | 18.7 | 180 | 3600 | male | 2007 | ||
| Dream | 37.2 | 18.1 | 178 | 3900 | male | 2007 | ||
| mean | 38.5 | 18.0 | ||||||
| Chinstrap | Dream | 46.5 | 17.9 | 192 | 3500 | female | 2007 | |
| Dream | 50.0 | 19.5 | 196 | 3900 | male | 2007 | ||
| mean | 48.2 | 18.7 | ||||||
| Gentoo | Biscoe | 50.0 | 16.3 | 230 | 5700 | male | 2007 | |
| Biscoe | 46.1 | 13.2 | 211 | 4500 | female | 2007 | ||
| mean | 48.0 | 14.8 |
The summary cells are subject to FORMAT when using plain identifiers, along with the body rows.
| island | bill_len | bill_dep | flipper_len | body_mass | sex | year | ||
|---|---|---|---|---|---|---|---|---|
| Adelie | Dream | 39.50 | 16.70 | 178 | 3250 | female | 2007 | |
| Torgersen | 39.50 | 17.40 | 186 | 3800 | female | 2007 | ||
| Biscoe | 37.80 | 18.30 | 174 | 3400 | female | 2007 | ||
| Torgersen | 39.10 | 18.70 | 181 | 3750 | male | 2007 | ||
| Biscoe | 37.70 | 18.70 | 180 | 3600 | male | 2007 | ||
| Dream | 37.20 | 18.10 | 178 | 3900 | male | 2007 | ||
| mean | — | 38.47 | 17.98 | — | — | — | — | |
| Chinstrap | Dream | 46.50 | 17.90 | 192 | 3500 | female | 2007 | |
| Dream | 50.00 | 19.50 | 196 | 3900 | male | 2007 | ||
| mean | — | 48.25 | 18.70 | — | — | — | — | |
| Gentoo | Biscoe | 50.00 | 16.30 | 230 | 5700 | male | 2007 | |
| Biscoe | 46.10 | 13.20 | 211 | 4500 | female | 2007 | ||
| mean | — | 48.05 | 14.75 | — | — | — | — |
To specifically format the summary values, you can use the expression string form.
| island | bill_len | bill_dep | flipper_len | body_mass | sex | year | ||
|---|---|---|---|---|---|---|---|---|
| Adelie | Dream | 39.5 | 16.7 | 178 | 3250 | female | 2007 | |
| Torgersen | 39.5 | 17.4 | 186 | 3800 | female | 2007 | ||
| Biscoe | 37.8 | 18.3 | 174 | 3400 | female | 2007 | ||
| Torgersen | 39.1 | 18.7 | 181 | 3750 | male | 2007 | ||
| Biscoe | 37.7 | 18.7 | 180 | 3600 | male | 2007 | ||
| Dream | 37.2 | 18.1 | 178 | 3900 | male | 2007 | ||
| 📏 mean | — | 📏 38.47 | 📏 17.98 | — | — | — | — | |
| Chinstrap | Dream | 46.5 | 17.9 | 192 | 3500 | female | 2007 | |
| Dream | 50.0 | 19.5 | 196 | 3900 | male | 2007 | ||
| 📏 mean | — | 📏 48.25 | 📏 18.70 | — | — | — | — | |
| Gentoo | Biscoe | 50.0 | 16.3 | 230 | 5700 | male | 2007 | |
| Biscoe | 46.1 | 13.2 | 211 | 4500 | female | 2007 | ||
| 📏 mean | — | 📏 48.05 | 📏 14.75 | — | — | — | — |
You can use repeated summaries. The order in which the summaries are declared determine the order of the rows.
| species | island | bill_len | bill_dep | flipper_len | body_mass | sex | year | |
|---|---|---|---|---|---|---|---|---|
| Adelie | Dream | 39.5 | 16.7 | 178 | 3250 | female | 2007 | |
| Gentoo | Biscoe | 50.0 | 16.3 | 230 | 5700 | male | 2007 | |
| Chinstrap | Dream | 46.5 | 17.9 | 192 | 3500 | female | 2007 | |
| Adelie | Torgersen | 39.5 | 17.4 | 186 | 3800 | female | 2007 | |
| Adelie | Biscoe | 37.8 | 18.3 | 174 | 3400 | female | 2007 | |
| Adelie | Torgersen | 39.1 | 18.7 | 181 | 3750 | male | 2007 | |
| Adelie | Biscoe | 37.7 | 18.7 | 180 | 3600 | male | 2007 | |
| Adelie | Dream | 37.2 | 18.1 | 178 | 3900 | male | 2007 | |
| Gentoo | Biscoe | 46.1 | 13.2 | 211 | 4500 | female | 2007 | |
| Chinstrap | Dream | 50.0 | 19.5 | 196 | 3900 | male | 2007 | |
| p25 | — | — | 38.1 | 16.9 | — | — | — | — |
| p50 | — | — | 39.5 | 18.0 | — | — | — | — |
| p75 | — | — | 46.4 | 18.6 | — | — | — | — |
Or display related summaries in a single cell.
| species | island | bill_len | bill_dep | flipper_len | body_mass | sex | year | |
|---|---|---|---|---|---|---|---|---|
| Adelie | Dream | 39.5 | 16.7 | 178 | 3250 | female | 2007 | |
| Gentoo | Biscoe | 50.0 | 16.3 | 230 | 5700 | male | 2007 | |
| Chinstrap | Dream | 46.5 | 17.9 | 192 | 3500 | female | 2007 | |
| Adelie | Torgersen | 39.5 | 17.4 | 186 | 3800 | female | 2007 | |
| Adelie | Biscoe | 37.8 | 18.3 | 174 | 3400 | female | 2007 | |
| Adelie | Torgersen | 39.1 | 18.7 | 181 | 3750 | male | 2007 | |
| Adelie | Biscoe | 37.7 | 18.7 | 180 | 3600 | male | 2007 | |
| Adelie | Dream | 37.2 | 18.1 | 178 | 3900 | male | 2007 | |
| Gentoo | Biscoe | 46.1 | 13.2 | 211 | 4500 | female | 2007 | |
| Chinstrap | Dream | 50.0 | 19.5 | 196 | 3900 | male | 2007 | |
| p50 (p25, p75) | — | — | 39.50 (38.12, 46.40) | 18.00 (16.88, 18.60) | — | — | — | — |
When data contains ‘null’ values, they propagate to aggregate metrics (but not necessarily count metrics). Notice how the summary of bill_len comes out as ‘null’.
| species | island | sex | bill_len | bill_dep | flipper_len | body_mass | |
|---|---|---|---|---|---|---|---|
| Adelie | Dream | female | 39.5 | 16.7 | 178 | 3250 | |
| Gentoo | Biscoe | male | null | 16.3 | 230 | 5700 | |
| Chinstrap | Dream | female | 46.5 | 17.9 | 192 | 3500 | |
| Adelie | Torgersen | female | 39.5 | 17.4 | 186 | 3800 | |
| Adelie | Biscoe | female | null | 18.3 | 174 | 3400 | |
| Adelie | Torgersen | male | 39.1 | 18.7 | 181 | 3750 | |
| Adelie | Biscoe | male | null | 18.7 | 180 | 3600 | |
| Adelie | Dream | male | 37.2 | 18.1 | 178 | 3900 | |
| Gentoo | Biscoe | female | null | 13.2 | 211 | 4500 | |
| Chinstrap | Dream | male | 50.0 | 19.5 | 196 | 3900 | |
| mean | — | — | — | null | 17.48 | — | — |
You can ignore ‘null’ values by using the missing => 'drop' setting.
| species | island | sex | bill_len | bill_dep | flipper_len | body_mass | |
|---|---|---|---|---|---|---|---|
| Adelie | Dream | female | 39.5 | 16.7 | 178 | 3250 | |
| Gentoo | Biscoe | male | null | 16.3 | 230 | 5700 | |
| Chinstrap | Dream | female | 46.5 | 17.9 | 192 | 3500 | |
| Adelie | Torgersen | female | 39.5 | 17.4 | 186 | 3800 | |
| Adelie | Biscoe | female | null | 18.3 | 174 | 3400 | |
| Adelie | Torgersen | male | 39.1 | 18.7 | 181 | 3750 | |
| Adelie | Biscoe | male | null | 18.7 | 180 | 3600 | |
| Adelie | Dream | male | 37.2 | 18.1 | 178 | 3900 | |
| Gentoo | Biscoe | female | null | 13.2 | 211 | 4500 | |
| Chinstrap | Dream | male | 50.0 | 19.5 | 196 | 3900 | |
| mean | — | — | — | 41.97 | 17.48 | — | — |
Date and timestamp columns can be summarised with the order statistics, formatted with :time, and range gives their span as a duration.
CREATE TEMP TABLE launches AS
SELECT * FROM (VALUES
('Alpha', DATE '2024-01-15'),
('Alpha', DATE '2024-02-10'),
('Beta', DATE '2024-03-01'),
('Beta', DATE '2024-03-20')) AS t(product, launched)
TABULATE * FROM launches
FACET product
SUMMARISE '{min:time %d %b %Y} – {max}' OF launched
SETTING rowlabel => 'Dates'
SUMMARISE '{range:duration d}' OF launched
SETTING rowlabel => 'Duration'| launched | ||
|---|---|---|
| Alpha | 2024-01-15 | |
| 2024-02-10 | ||
| Dates | 15 Jan 2024 – 2024-02-10 | |
| Duration | 26d | |
| Beta | 2024-03-01 | |
| 2024-03-20 | ||
| Dates | 01 Mar 2024 – 2024-03-20 | |
| Duration | 19d |