# Summarise only rows that meet a condition

Shape / table

A variant of [Summarise by group](https://meridian.online/operations/summarise-by-group).

Compute an aggregate over just the rows of each group that satisfy a condition, alongside unfiltered aggregates in the same row.

Page: <https://meridian.online/operations/summarise-by-group/conditional-aggregate>

## Status

Runs as a SQL step.

arcform carries it as a `sql:` step today.

## The DuckDB step

summarise only the rows of each group that meet a condition, next to unfiltered aggregates

It reads the `orders` table the proofs share. Create it first:

```sql
CREATE TABLE orders AS SELECT * FROM (VALUES
  (1,  'Alice Ng',      'north', DATE '2026-01-03', 120.00, 'widget', 'a|b',   '{"channel":"web","items":[1,2]}'),
  (2,  'alice ng',      'north', DATE '2026-01-05',  80.50, 'gadget', 'b',     '{"channel":"shop","items":[3]}'),
  (3,  'Bob  Okafor',   'south', DATE '2026-01-05', 300.00, 'widget', 'a|c',   '{"channel":"web","items":[4,5,6]}'),
  (4,  'Carla Díaz',    'east',  DATE '2026-01-09',  NULL,  'gizmo',  NULL,    '{"channel":"web","items":[]}'),
  (5,  'Bob Okafor',    'south', DATE '2026-01-12',  45.25, 'gadget', 'c',     '{"channel":"phone","items":[7]}'),
  (6,  'Dev Patel',     'west',  DATE '2026-01-12', 510.00, 'widget', 'a',     '{"channel":"web","items":[8,9]}'),
  (7,  'Carla Diaz',    'east',  DATE '2026-01-20',  99.99, 'gizmo',  'b|c',   '{"channel":"shop","items":[10]}'),
  (8,  'Dev Patel',     'west',  DATE '2026-02-02', 510.00, 'widget', 'a',     '{"channel":"web","items":[8,9]}'),
  (9,  'Eve Larsen',    NULL,    DATE '2026-02-03',  15.00, 'gadget', NULL,    '{"channel":"web","items":[11]}'),
  (10, 'Alice Ng',      'north', DATE '2026-02-10', 220.00, 'gizmo',  'a|b|c', '{"channel":"phone","items":[12,13]}')
) t(order_id, customer, region, order_date, amount, product, tags, meta);
```

Then run the statement:

```sql
CREATE TEMP TABLE result AS
SELECT product,
       count(*) AS orders,
       sum(amount) AS total,
       count(*) FILTER (WHERE amount >= 100) AS big_orders,
       sum(amount) FILTER (WHERE amount >= 100) AS big_total,
       sum(CASE WHEN amount >= 100 THEN amount END) AS big_total_case
FROM orders
GROUP BY product;
SELECT * FROM result ORDER BY product;
SELECT CASE WHEN ((SELECT list((product, orders, total, big_orders, big_total) ORDER BY product) FROM result) IS NOT DISTINCT FROM [('gadget', 3, 140.75, 0, NULL), ('gizmo', 3, 319.99, 1, 220.00), ('widget', 4, 1440.00, 4, 1440.00)] AND (SELECT bool_and(big_total IS NOT DISTINCT FROM big_total_case) FROM result)) THEN 'as expected' ELSE error('the filtered and unfiltered aggregates did not sit side by side: gadget has no big order, gizmo one of 220.00, widget four') END AS verdict;
```

It needs nothing installed beyond DuckDB itself.

## What other tools call it

Meridian's classification; an independent blind rating agreed on 95.7% of 324 rows (variant, version 3).

- Apache DataFusion: [`FILTER (WHERE ...)`](https://datafusion.apache.org/user-guide/sql/aggregate_functions.html) (read 2026-09-27)
- DuckDB: [`FILTER (clause)`](https://duckdb.org/docs/current/sql/query_syntax/filter) (read 2026-09-27)
- DuckDB: [`count_if`](https://duckdb.org/docs/current/sql/functions/aggregates) (read 2026-09-27)
- DuckDB: [`countif`](https://duckdb.org/docs/current/sql/functions/aggregates) (read 2026-09-27)
- Trifacta: [`ANYIF Function`](https://help.alteryx.com/aac/en/trifacta-classic/wrangle-language/aggregate-functions.html) (read 2026-09-27)
- Trifacta: [`AVERAGEIF Function`](https://help.alteryx.com/aac/en/trifacta-classic/wrangle-language/aggregate-functions.html) (read 2026-09-27)
- Trifacta: [`COUNTAIF Function`](https://help.alteryx.com/aac/en/trifacta-classic/wrangle-language/aggregate-functions.html) (read 2026-09-27)
- Trifacta: [`COUNTDISTINCTIF Function`](https://help.alteryx.com/aac/en/trifacta-classic/wrangle-language/aggregate-functions.html) (read 2026-09-27)
- Trifacta: [`COUNTIF Function`](https://help.alteryx.com/aac/en/trifacta-classic/wrangle-language/aggregate-functions.html) (read 2026-09-27)
- Trifacta: [`KTHLARGESTIF Function`](https://help.alteryx.com/aac/en/trifacta-classic/wrangle-language/aggregate-functions.html) (read 2026-09-27)
- Trifacta: [`KTHLARGESTUNIQUEIF Function`](https://help.alteryx.com/aac/en/trifacta-classic/wrangle-language/aggregate-functions.html) (read 2026-09-27)
- Trifacta: [`LISTIF Function`](https://help.alteryx.com/aac/en/trifacta-classic/wrangle-language/aggregate-functions.html) (read 2026-09-27)
- Trifacta: [`MAXIF Function`](https://help.alteryx.com/aac/en/trifacta-classic/wrangle-language/aggregate-functions.html) (read 2026-09-27)
- Trifacta: [`MINIF Function`](https://help.alteryx.com/aac/en/trifacta-classic/wrangle-language/aggregate-functions.html) (read 2026-09-27)
- Trifacta: [`MODEIF Function`](https://help.alteryx.com/aac/en/trifacta-classic/wrangle-language/aggregate-functions.html) (read 2026-09-27)
- Trifacta: [`STDEVIF Function`](https://help.alteryx.com/aac/en/trifacta-classic/wrangle-language/aggregate-functions.html) (read 2026-09-27)
- Trifacta: [`STDEVSAMPIF Function`](https://help.alteryx.com/aac/en/trifacta-classic/wrangle-language/aggregate-functions.html) (read 2026-09-27)
- Trifacta: [`SUMIF Function`](https://help.alteryx.com/aac/en/trifacta-classic/wrangle-language/aggregate-functions.html) (read 2026-09-27)
- Trifacta: [`VARIF Function`](https://help.alteryx.com/aac/en/trifacta-classic/wrangle-language/aggregate-functions.html) (read 2026-09-27)
- Trifacta: [`VARSAMPIF Function`](https://help.alteryx.com/aac/en/trifacta-classic/wrangle-language/aggregate-functions.html) (read 2026-09-27)
