--- spec: openflange.org/pipe/v0.1 id: running-total version: 0.1.0 title: Running total and rank (DuckDB SQL window functions) description: Adds a per-customer running total and an overall rank by amount to every order, using SQL analytical window functions in DuckDB. license: Apache-2.0 keywords: [duckdb, sql, window-function, running-total, rank, example] maturity: experimental scope: dataset deterministic: true inputs: orders: description: 'Orders: {"customer": string, "day": "YYYY-MM-DD", "amount": number}.' schema: format: json-schema definition: {type: object} outputs: ranked: description: 'Every order, ordered by customer then day: {"customer", "day", "amount", "running_total", "amount_rank"}.' schema: format: json-schema definition: type: object required: [customer, day, amount, running_total, amount_rank] properties: amount_rank: {type: integer, minimum: 1} implementation: type: executable command: ["duckdb", "-no-init", "-bail", "-f", "query.sql"] stdin: orders stdout: ranked permissions: subprocess: true --- # Running total and rank (DuckDB SQL window functions) Implements the [OpenFlange](https://openflange.org/) specification. The `duckdb` command-line shell used as an executable pipe: NDJSON on stdin, the query in [`query.sql`](query.sql), NDJSON on stdout. See [../README.md](../README.md) for input → command → output. - **Dataset scope — the input is materialised.** DuckDB reads all of stdin before the first row comes out. **SQL window functions are not stream windows**: `OVER (...)` is evaluated over the whole materialised dataset, whereas an OpenFlange `windows` stream (`std:window`) groups records as they flow, in bounded memory. - **Input columns are declared in the query** (`customer` text, `day` date, `amount` decimal(18,2)); other members are ignored. Empty input gives empty output. A value that cannot be converted (`"amount":"abc"`) fails the run with `E_RUNTIME`. A missing or null `amount` is SQL `NULL`, with SQL's usual meaning (window `sum` skips it; it ranks last). - **Permissions:** `subprocess` (it starts `duckdb`; in-memory database, no files, no network, `-no-init` so no user start-up file is read). A subprocess is not a sandbox. - **Pinned:** verified with DuckDB v2.0.1.