# The Same DuckDB Table, Two Definitions, Two Opposite Trends

> Source: <https://dev.to/yvonneyx/the-same-duckdb-table-two-definitions-two-opposite-trends-3gjd>
> Published: 2026-12-01 17:02:48+00:00

**Recommendation:** Write the metric definition explicitly into each SQL statement and cross-check totals with a second independent pass before trusting any trend.
More in the playbook "Verifying model output": <https://wpnews.pro/playbooks/output-verification>

DuckDB is often the right tool when you want local SQL over files, without standing up a database server. This walkthrough stays in that world: one CSV, explicit DuckDB SQL, and totals you can check.

The catch is not the engine. It is the metric.

I used AVA's official synthetic retail dataset — 18,971 line items, 15,510 orders, snapshot date 2026-10-05 — from the [`retail-semantics`](https://github.com/antvis/AVA/tree/ai/data/retail-semantics) folder on the `ai` branch of [antvis/AVA](https://github.com/antvis/AVA). Under the project's finance definition, net sales fell from CNY 4,293,025.42 in August 2026 to CNY 3,697,575.18 in September, a drop of CNY 595,450.24, or 13.87%. Under the trading definition, net sales rose from CNY 6,740,687.05 to CNY 7,803,998.06, an increase of CNY 1,063,311.01, or 15.77%.

Those totals are published in the dataset README and were independently cross-checked there. I reproduced them in two ways on 2 December 2026: a direct pass over the CSV in Python, and AVA's CLI, which runs the queries on its default Node.js engine, DuckDB. Both routes matched.

This is a follow-up to [Give Your Coding Agent Inspectable Data Analysis Without a Second Model Key](https://dev.to/yvonneyx/give-your-coding-agent-inspectable-data-analysis-without-a-second-model-key-466m), which used an 8-row sample. The idea is the same: if you cannot read the SQL, you cannot trust the trend.

The file is synthetic. It is not a real company. It also has no inventory, traffic, or advertising events, so a change in net sales is not evidence of a stockout or a campaign.

[AVA](https://ava.antv.vision) 4.0 is an open-source TypeScript framework from AntV for data analysis and visualization in JavaScript applications and AI agents. In the Node.js SDK, omitting `engine` selects DuckDB. The engine creates an in-memory DuckDB instance, registers your sources as tables or views, and executes SQL you provide with `query` (or `ava query` on the CLI).

That is documented in the [engine overview](https://ava.antv.vision/documentation/engines/overview/) and the [DuckDB engine guide](https://ava.antv.vision/documentation/engines/duckdb/). This walkthrough used `@antv/ava@4.0.5`, which depends on `@duckdb/node-api`. A local install on 2 December 2026 resolved `@duckdb/node-api@2.0.0-r.1`. I am not treating that npm tag as identical to a specific DuckDB LTS blog announcement.

`query` does not call a model and does not rewrite your SQL from a semantic file. If you want the finance or trading definition, it has to appear in the statement you run. Commands such as `analyze` and `suggest` are different: they need model configuration. The earlier article covers that boundary.

DuckDB inside AVA is also not a full-privilege SQL shell. The engine guide describes several safeguards for model-generated SQL, and they apply here too:

`SELECT` (including `WITH`); writes and multi-statement queries are rejected` maxRows` / `maxResultBytes` on returned rows
The CSV is about 4.6 MB. Download it from the official repository (default branch name: `ai`):

```
curl -fsSL -o sales.csv \
  https://raw.githubusercontent.com/antvis/AVA/ai/data/retail-semantics/sales.csv
```

You need Node.js 22.13 or later. You can install the CLI globally with `npm install -g @antv/ava`, or use a project-local binary:

```
npm install @antv/ava@4.0.5
npx ava source sales.csv > session.json
DATASET_ID=$(node -p 'JSON.parse(require("fs").readFileSync("session.json", "utf8")).datasetId')
npx ava schema "$DATASET_ID" > schema.json
```

Redirecting `source` matters: the CLI prints a guide in a terminal and JSON when piped or redirected.

Read `schema.json` before writing SQL. For this single CSV, the DuckDB loader exposes one table named `data`. The schema language is `duckdb-sql`. There are 29 columns, including `paid_at`, `completed_at`, `status`, `order_kind`, `business_model`, `selling_store_name`, `channel`, `product_name`, `line_paid_amount`, `refund_amount`, and `commission_rate`.

In this snapshot, every `self_operated` line has `commission_rate` of `1`, and every `consignment` line has `0.03`. That is why the official finance formula — which always multiplies by `commission_rate` — matches the prose that company-owned lines recognize the full paid amount minus refund, while consignment lines recognize commission only. Do not assume those rates on another file; read the data.

Both definitions come from the published semantic files in the same folder. I am quoting the rules and the `net_revenue` expressions, not inventing a third metric.

**Finance** (`semantic-finance.json`): net sales are recognized revenue on external customer orders that have been fulfilled. Keep `order_kind = 'customer_sale'` and `status = 'completed'`. Attribute the line to the calendar month of `completed_at`. Refunds in the 2026-10-05 snapshot are applied back to the original fulfillment month, not to `refunded_at`.

The official `net_revenue` expression is:

```
SUM(
  CASE
    WHEN order_kind = 'customer_sale' AND status IN ('completed') THEN (
      ROUND(CAST(line_paid_amount AS DECIMAL(18, 2)) * CAST(commission_rate AS DECIMAL(6, 4)), 2)
      - ROUND(COALESCE(CAST(refund_amount AS DECIMAL(18, 2)), 0) * CAST(commission_rate AS DECIMAL(6, 4)), 2)
    )
    ELSE 0
  END
)
```

**Trading** (`semantic-trading.json`): net sales are net payments from external customers. Keep `order_kind = 'customer_sale'` and `status` in `completed`, `shipped`, or `paid`. Attribute the line to the calendar month of `paid_at`. Company-owned and consignment lines both use the full line payment minus refund, with no commission multiplier. Paid but not-yet-fulfilled orders are included. Refunds are applied back to the original payment month.

```
SUM(
  CASE
    WHEN order_kind = 'customer_sale' AND status IN ('completed', 'shipped', 'paid') THEN (
      CAST(line_paid_amount AS DECIMAL(18, 2))
      - COALESCE(CAST(refund_amount AS DECIMAL(18, 2)), 0)
    )
    ELSE 0
  END
)
```

Attribution is also defined: use `selling_store_*` for sales responsibility. `fulfillment_*` is where the order was fulfilled, and a centralized warehouse should not reassign the selling store.

Save this as `query-finance-months.sql`:

```
SELECT
  date_trunc('month', CAST(completed_at AS DATE)) AS month,
  SUM(
    CASE
      WHEN order_kind = 'customer_sale' AND status IN ('completed') THEN (
        ROUND(CAST(line_paid_amount AS DECIMAL(18, 2)) * CAST(commission_rate AS DECIMAL(6, 4)), 2)
        - ROUND(COALESCE(CAST(refund_amount AS DECIMAL(18, 2)), 0) * CAST(commission_rate AS DECIMAL(6, 4)), 2)
      )
      ELSE 0
    END
  ) AS net_revenue
FROM data
WHERE CAST(completed_at AS DATE) >= DATE '2026-08-01'
  AND CAST(completed_at AS DATE) < DATE '2026-10-01'
GROUP BY 1
ORDER BY 1;
```

And this as `query-trading-months.sql`:

```
SELECT
  date_trunc('month', CAST(paid_at AS DATE)) AS month,
  SUM(
    CASE
      WHEN order_kind = 'customer_sale' AND status IN ('completed', 'shipped', 'paid') THEN (
        CAST(line_paid_amount AS DECIMAL(18, 2))
        - COALESCE(CAST(refund_amount AS DECIMAL(18, 2)), 0)
      )
      ELSE 0
    END
  ) AS net_revenue
FROM data
WHERE CAST(paid_at AS DATE) >= DATE '2026-08-01'
  AND CAST(paid_at AS DATE) < DATE '2026-10-01'
GROUP BY 1
ORDER BY 1;
npx ava query "$DATASET_ID" --dsl @query-finance-months.sql
npx ava query "$DATASET_ID" --dsl @query-trading-months.sql
```

You should see:

| Definition | August (CNY) | September (CNY) | Change (CNY) | Change | 
|---|---|---|---|---|
| Finance | 4,293,025.42 | 3,697,575.18 | −595,450.24 | −13.87% | 
| Trading | 6,740,687.05 | 7,803,998.06 | +1,063,311.01 | +15.77% | 

The CLI returns `month` as a timestamp, such as `2026-08-01 00:00:00`. The net revenue type in this run was `DECIMAL(38,2)`.

Nothing in DuckDB "chose" a trend. The two statements include different statuses, use different date columns, and treat consignment income differently. If an agent only prints "sales were down in September," you cannot tell which of those choices it made.

The finance semantic file says to locate change by absolute amount, keep offsetting gains, and use selling store, channel, and product together. This query lists the five largest finance declines at that grain:

```
WITH monthly AS (
  SELECT
    selling_store_name,
    channel,
    product_name,
    date_trunc('month', CAST(completed_at AS DATE)) AS month,
    SUM(
      CASE
        WHEN order_kind = 'customer_sale' AND status IN ('completed') THEN (
          ROUND(CAST(line_paid_amount AS DECIMAL(18, 2)) * CAST(commission_rate AS DECIMAL(6, 4)), 2)
          - ROUND(COALESCE(CAST(refund_amount AS DECIMAL(18, 2)), 0) * CAST(commission_rate AS DECIMAL(6, 4)), 2)
        )
        ELSE 0
      END
    ) AS net_revenue
  FROM data
  WHERE CAST(completed_at AS DATE) >= DATE '2026-08-01'
    AND CAST(completed_at AS DATE) < DATE '2026-10-01'
  GROUP BY 1, 2, 3, 4
)
SELECT
  selling_store_name,
  channel,
  product_name,
  SUM(CASE WHEN month = DATE '2026-08-01' THEN net_revenue ELSE 0 END) AS august_cny,
  SUM(CASE WHEN month = DATE '2026-09-01' THEN net_revenue ELSE 0 END) AS september_cny,
  SUM(CASE WHEN month = DATE '2026-09-01' THEN net_revenue ELSE 0 END)
    - SUM(CASE WHEN month = DATE '2026-08-01' THEN net_revenue ELSE 0 END) AS change_cny
FROM monthly
GROUP BY 1, 2, 3
ORDER BY change_cny
LIMIT 5;
```

The first row is 深圳南山店 / 线上商城 / 电动升降桌: August CNY 1,790,783.35, September CNY 1,232,006.98, change CNY −558,776.37. That single combination accounts for most of the CNY 595,450.24 finance decline. Grouping only by `selling_store_name` shows that 广州天河店 moved the other way, from CNY 518,590.81 to CNY 585,304.12, or +CNY 66,713.31, which offsets part of the drop. Both figures match the dataset README.

The file still cannot tell you why those combinations moved. There is no stock or media table to join.

```
npx ava dispose "$DATASET_ID"
```

Sessions expire after 30 idle minutes. If that happens, load the source again and use the new dataset ID.

It is not a benchmark of DuckDB, and it is not a claim that AVA replaces the DuckDB CLI for every workflow. If you already have a DuckDB script, you can run the same SQL there and compare. AVA's value in this path is a Node.js session around DuckDB, with inspectable `query` results, and — separately, when you want it — an agent skill that is supposed to show you the statement it ran.

It is also not a head-to-head with [PandasAI](https://docs.pandas-ai.com/v3/introduction). PandasAI is a Python library for asking questions of data in natural language. AVA's README describes AVA as a JavaScript-ecosystem counterpart, but there is no official PandasAI Node.js SDK in the PandasAI documentation I reviewed, and this article does not run both tools on the same question. A fair comparison belongs in a later piece, with documented features and no invented integration.

This article was drafted with AI assistance and reviewed against AVA's official documentation and the published `retail-semantics` files. The queries were executed locally with `@antv/ava@4.0.5` on 2 December 2026, and the monthly totals were also recomputed directly from the CSV. I am affiliated with work on AVA's developer content.
