Cloudflare D1 icon

Cloudflare D1

Execute operations against Cloudflare D1 serverless databases. Supports structured operations (Insert, Select, Update, Delete), raw SQL queries, and can be used as a tool by AI Agents.

Query Builder → Aggregate Query

AI-generated

Summary

Build and execute an aggregate query on a Cloudflare D1 table, optionally grouping and filtering rows.

Inputs

  • databaseId (required) — The unique identifier of your Cloudflare D1 database.
  • builderTable (required) — The table to query.
  • whereConditions — Conditions to filter rows before aggregation.
  • aggregateFunction — Aggregate function to apply (e.g., AVG, COUNT, MAX, MIN, SUM).
  • aggregateColumn — Column to aggregate (leave empty for COUNT(*)).
  • groupBy — Columns to group by.

Output shape

array of aggregated rows

Each row contains the grouping columns (if any) and the result of the selected aggregate function; may include additional metadata such as row count.

Examples

Example 1: Count all rows in the users table.

databaseId = $databaseId, builderTable = 'users', aggregateFunction = 'COUNT', aggregateColumn left empty, groupBy empty

Example 2: Sum of price per category where price is greater than 100.

databaseId = 'db123', builderTable = 'orders', aggregateFunction = 'SUM', aggregateColumn = 'price', groupBy = ['category'], whereConditions = [{field:'price',operator:'>',value:'100'}]

Discussion