> ## Documentation Index
> Fetch the complete documentation index at: https://zenskar.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Single-concept walkthrough: How the SQL builder works

> This example-based document shows how a billable metric moves from the visual builder to the SQL builder, and how its SQL query gives one result for each region of one customer.

## Story: counting the API calls of Acme Corp by region

Your company sells access to a messaging API. It plans to charge a different price for the API calls of each region.

Your billing team already meters the API calls of Acme Corp in the [usage-event collection](https://zenskar.com/docs/20260301/zenskar-modules/usage/glossary.md#usage-event-collection) `API calls`. Each [usage event](https://zenskar.com/docs/20260301/zenskar-modules/usage/glossary.md#usage-event) now also reports the region of its calls. The billing team needs one number of calls for each region. The [visual builder](https://zenskar.com/docs/20260301/zenskar-modules/usage/glossary.md#visual-builder) gives one number for each customer and period, so it cannot give a number for each region.

The billing team starts a [billable metric](https://zenskar.com/docs/20260301/zenskar-modules/usage/glossary.md#billable-metric) in the visual builder. It then moves the billable metric to the [SQL builder](https://zenskar.com/docs/20260301/zenskar-modules/usage/glossary.md#sql-builder), and changes the SQL query to give one number for each region. On the way, it puts these concepts into practice:

* [The visual builder](https://zenskar.com/docs/20260301/zenskar-modules/usage/concepts/billable-metrics.md#visual-builder-a-billable-metric-in-four-steps), and the SQL query that it builds
* [The SQL builder](https://zenskar.com/docs/20260301/zenskar-modules/usage/concepts/billable-metrics.md#sql-builder-a-billable-metric-as-an-sql-query), with the `quantity` column and a [dimension](https://en.wikipedia.org/wiki/Dimension_\(data_warehouse\))
* [Zenskar SQL variables](https://zenskar.com/docs/20260301/zenskar-modules/usage/concepts/billable-metrics.md#zenskar-sql-variables-values-that-zenskar-fills-in)
* [Results for each customer and period](https://zenskar.com/docs/20260301/zenskar-modules/usage/concepts/billable-metrics.md#results-for-each-customer-and-period)

The story uses these values:

| Value | In the story |
| - | - |
| Usage-event collection | `API calls`, with the [API slug](https://zenskar.com/docs/20260301/zenskar-modules/usage/glossary.md#api-slug) `api_calls`. Its table in a query is `raw_metric_api_calls`. |
| [Data fields](https://zenskar.com/docs/20260301/zenskar-modules/usage/glossary.md#data-field) | `calls`, with the data type Int64, and `region`, with the data type String |
| Customer | Acme Corp, with the external ID `acme-corp` |
| Usage events | On November 3, 2026: 300 calls in `eu-west`, 200 calls in `eu-west`, and 400 calls in `us-east` |
| Billable metric | `API calls by region`, the sum of `calls` for each region |
| Period | November 1 to November 30, 2026 |

The company, the customer, and the numbers are fictional.

### Prerequisites

* A role with the **Can Create Billable Metric** and **Can Read Billable Metric** permissions.
* The usage-event collection `API calls`, with the data field `region`. See [Add a data field](https://zenskar.com/docs/20260301/zenskar-modules/usage/how-to-guides/usage-events.md#add-a-data-field).
* The three usage events of Acme Corp in November 2026. See [Send usage events through the API](https://zenskar.com/docs/20260301/zenskar-modules/usage/how-to-guides/usage-events.md#send-usage-events-through-the-api).
* A customer with the external ID `acme-corp`.

### Start the billable metric in the visual builder

1. Go to **Usage** > **Billable Metrics**.

2. Click **ADD NEW BILLABLE METRIC**. The **Add New Billable Metric** screen opens with **VISUAL BUILDER** selected.

   The billing team could write the whole SQL query in the SQL builder. It starts in the [visual builder](https://zenskar.com/docs/20260301/zenskar-modules/usage/concepts/billable-metrics.md#visual-builder-a-billable-metric-in-four-steps), because the visual builder writes the parts of the query that every billable metric of `API calls` needs: the table, the customer, and the period. The billing team then changes only what the visual builder cannot give.

3. Under **Select tables & filters**, in **Table Name**, select `api_calls` under **Usage Event**. Click **NEXT**.

4. Under **Map customer**, keep `customer_id` in **Your customer column** and **external\_id** in **Zenskar customer column**. Click **NEXT**.

5. Under **Set date field**, keep `timestamp` in **Column**. Click **NEXT**.

6. Under **Set Billable Metric over**, in **Select calculation type**, select **SUM**. In **Select column to calculate over**, select `calls`.

   Zenskar converts each setting of the visual builder into a part of an SQL query:

   | Setting | Part of the SQL query |
   | - | - |
   | The table `api_calls` | `FROM "raw_metric_api_calls"` |
   | `customer_id`, matched to the external ID | `"customer_id" = CAST({{customer.external_id}} AS String)` |
   | The date column `timestamp` | `DATE("timestamp") >= DATE({{start_date}})` and `DATE("timestamp") <= DATE({{end_date}})` |
   | The sum of `calls` | `COALESCE(SUM("data.calls"), 0) AS "quantity"` |

### Move the billable metric to the SQL builder

1. Click **SQL BUILDER**. The editor shows the SQL query of the settings, read-only:

   ```sql theme={null}
   SELECT
       COALESCE(SUM("data.calls"), 0) AS "quantity"
   FROM
       "raw_metric_api_calls"
   WHERE
       DATE("timestamp") >= DATE({{start_date}}) AND
       DATE("timestamp") <= DATE({{end_date}}) AND
       "customer_id" = CAST({{customer.external_id}} AS String)
   LIMIT
       10
   OFFSET
       0
   ```

   `{{customer.external_id}}`, `{{start_date}}`, and `{{end_date}}` are [Zenskar SQL variables](https://zenskar.com/docs/20260301/zenskar-modules/usage/concepts/billable-metrics.md#zenskar-sql-variables-values-that-zenskar-fills-in). Each time Zenskar runs the billable metric, it fills them in with the external ID of the customer and the first and last days of the period. The query returns one value, named `quantity`, so it gives one number for each customer and period.

2. Click **EDIT**. The **SQL Builder Edit Access** window opens.

   In the [SQL builder](https://zenskar.com/docs/20260301/zenskar-modules/usage/concepts/billable-metrics.md#sql-builder-a-billable-metric-as-an-sql-query), the billing team changes the query itself. After the move, the settings of the visual builder no longer apply to the billable metric.

3. Click **YES**. The editor becomes editable, and **VISUAL BUILDER** is no longer available.

   > [!CAUTION]
   >
   > The move is one way: after the billable metric is saved as a query, it cannot return to the visual builder. Move a billable metric only when it needs a query that the visual builder cannot build, such as one with dimensions.

### Add the region as a dimension

1. In the editor, after `SELECT`, add the line `"data.region" AS "region",`.

   The data field `region` is the column `data.region` of the table `raw_metric_api_calls`. See [Usage events as rows and columns](https://zenskar.com/docs/20260301/zenskar-modules/usage/concepts/usage-events.md#usage-events-as-rows-and-columns). The new line adds a column named `region` to the result of the query.

2. Replace the lines from `LIMIT` to the end of the query with `GROUP BY "data.region"`. The query now reads:

   ```sql theme={null}
   SELECT
       "data.region" AS "region",
       COALESCE(SUM("data.calls"), 0) AS "quantity"
   FROM
       "raw_metric_api_calls"
   WHERE
       DATE("timestamp") >= DATE({{start_date}}) AND
       DATE("timestamp") <= DATE({{end_date}}) AND
       "customer_id" = CAST({{customer.external_id}} AS String)
   GROUP BY
       "data.region"
   ```

   `GROUP BY "data.region"` gives one row for each region, with the sum of the calls of that region.

   The query of the visual builder ended with `LIMIT 10`, which keeps at most 10 rows.

   > [!WARNING]
   >
   > Zenskar runs the query as written, including its `LIMIT`. A query with a dimension and `LIMIT 10` gives at most 10 combinations of [dimension values](https://zenskar.com/docs/20260301/zenskar-modules/products/glossary.md#dimension-value), without a message. Remove the limit when you add a dimension.

   The query now returns two columns. In the [SQL builder](https://zenskar.com/docs/20260301/zenskar-modules/usage/concepts/billable-metrics.md#sql-builder-a-billable-metric-as-an-sql-query), the column `quantity` holds the number, and each other column is a dimension. The billable metric therefore gives one number for each combination of dimension values. Here the only dimension is `region`, so each combination is one region, such as `eu-west`.

   The [Zenskar SQL variables](https://zenskar.com/docs/20260301/zenskar-modules/usage/concepts/billable-metrics.md#zenskar-sql-variables-values-that-zenskar-fills-in) stay in the query. The billable metric still gives its result for one customer and one period at a time.

### Preview the billable metric

Before saving, the billing team checks the result for Acme Corp in November.

1. Under **Query preview**, in **Search By Customer Name, External ID**, select Acme Corp.

2. In **Billing Period**, select November 1, 2026, as the first day and November 30, 2026, as the last day.

   The preview fills in the Zenskar SQL variables with the external ID of Acme Corp, `acme-corp`, and with the first and last days of November. See [Results for each customer and period](https://zenskar.com/docs/20260301/zenskar-modules/usage/concepts/billable-metrics.md#results-for-each-customer-and-period).

3. Click **RUN QUERY**. The table shows one row for each region:

   | `region` | `quantity` |
   | - | - |
   | `eu-west` | 500 |
   | `us-east` | 400 |

   Each row is one combination, with its number in `quantity`. The number of each region is the sum of the `calls` values of its usage events:

   | Usage event | `region` | `calls` |
   | - | - | - |
   | First | `eu-west` | 300 |
   | Second | `eu-west` | 200 |
   | Third | `us-east` | 400 |
   | Sum for `eu-west` | | 500 |
   | Sum for `us-east` | | 400 |

   A [product](https://zenskar.com/docs/20260301/zenskar-modules/products/glossary.md#product) can then charge a [combination price](https://zenskar.com/docs/20260301/zenskar-modules/products/glossary.md#combination-price) for each region.

### Save the billable metric

The result for Acme Corp matches its usage events, so the billing team saves the billable metric.

1. Click **CREATE BILLABLE METRIC**. The **Choose Billable Metric Name** window opens.
2. Enter `API calls by region`.
3. Click **CREATE BILLABLE METRIC**. The **Billable Metrics** list opens and shows `API calls by region`.

   Zenskar saves the billable metric as its SQL query. Each time Zenskar runs the billable metric, it runs the query for one customer and one period, and gives one number for each region.

### Summary: what your billing team did

Your billing team started the billable metric `API calls by region` in the visual builder, as the sum of `calls`. It moved the billable metric to the SQL builder. It then added `region` as a dimension, and replaced the `LIMIT` of the query with `GROUP BY` to keep every region. The preview gave 500 calls in `eu-west` and 400 calls in `us-east` for Acme Corp in November.

See [Create a billable metric](https://zenskar.com/docs/20260301/zenskar-modules/usage/how-to-guides/billable-metrics.md#create-a-billable-metric).

See [Move a billable metric to the SQL builder](https://zenskar.com/docs/20260301/zenskar-modules/usage/how-to-guides/billable-metrics.md#move-a-billable-metric-to-the-sql-builder).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.