{"slug": "the-same-duckdb-table-two-definitions-two-opposite-trends", "title": "The Same DuckDB Table, Two Definitions, Two Opposite Trends", "summary": "A developer reproduced AVA's synthetic retail dataset totals in DuckDB, showing that the same 18,971-line-item table yields opposite net-sales trends depending on the metric definition: finance net sales fell 13.87% from August to September 2026 (CNY 4,293,025.42 to 3,697,575.18), while trading net sales rose 15.77% (CNY 6,740,687.05 to 7,803,998.06). The totals, published in the dataset README, were cross-checked via a direct Python pass over the CSV and AVA's CLI, which runs queries on DuckDB through @antv/ava@4.0.5. The walkthrough stresses that AVA's query command does not call a model or rewrite SQL from a semantic file, so the finance or trading definition must appear explicitly in the statement executed.", "body_md": "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.\n\nThe catch is not the engine. It is the metric.\n\nI 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%.\n\nThose 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.\n\nThis 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.\n\nThe 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.\n\n[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).\n\nThat 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.\n\n`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.\n\nDuckDB 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:\n\n`SELECT` (including `WITH`); writes and multi-statement queries are rejected` maxRows` / `maxResultBytes` on returned rows\nThe CSV is about 4.6 MB. Download it from the official repository (default branch name: `ai`):\n\n```\ncurl -fsSL -o sales.csv \\\n  https://raw.githubusercontent.com/antvis/AVA/ai/data/retail-semantics/sales.csv\n```\n\nYou 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:\n\n```\nnpm install @antv/ava@4.0.5\nnpx ava source sales.csv > session.json\nDATASET_ID=$(node -p 'JSON.parse(require(\"fs\").readFileSync(\"session.json\", \"utf8\")).datasetId')\nnpx ava schema \"$DATASET_ID\" > schema.json\n```\n\nRedirecting `source` matters: the CLI prints a guide in a terminal and JSON when piped or redirected.\n\nRead `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`.\n\nIn 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.\n\nBoth 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.\n\n**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`.\n\nThe official `net_revenue` expression is:\n\n```\nSUM(\n  CASE\n    WHEN order_kind = 'customer_sale' AND status IN ('completed') THEN (\n      ROUND(CAST(line_paid_amount AS DECIMAL(18, 2)) * CAST(commission_rate AS DECIMAL(6, 4)), 2)\n      - ROUND(COALESCE(CAST(refund_amount AS DECIMAL(18, 2)), 0) * CAST(commission_rate AS DECIMAL(6, 4)), 2)\n    )\n    ELSE 0\n  END\n)\n```\n\n**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.\n\n```\nSUM(\n  CASE\n    WHEN order_kind = 'customer_sale' AND status IN ('completed', 'shipped', 'paid') THEN (\n      CAST(line_paid_amount AS DECIMAL(18, 2))\n      - COALESCE(CAST(refund_amount AS DECIMAL(18, 2)), 0)\n    )\n    ELSE 0\n  END\n)\n```\n\nAttribution 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.\n\nSave this as `query-finance-months.sql`:\n\n```\nSELECT\n  date_trunc('month', CAST(completed_at AS DATE)) AS month,\n  SUM(\n    CASE\n      WHEN order_kind = 'customer_sale' AND status IN ('completed') THEN (\n        ROUND(CAST(line_paid_amount AS DECIMAL(18, 2)) * CAST(commission_rate AS DECIMAL(6, 4)), 2)\n        - ROUND(COALESCE(CAST(refund_amount AS DECIMAL(18, 2)), 0) * CAST(commission_rate AS DECIMAL(6, 4)), 2)\n      )\n      ELSE 0\n    END\n  ) AS net_revenue\nFROM data\nWHERE CAST(completed_at AS DATE) >= DATE '2026-08-01'\n  AND CAST(completed_at AS DATE) < DATE '2026-10-01'\nGROUP BY 1\nORDER BY 1;\n```\n\nAnd this as `query-trading-months.sql`:\n\n```\nSELECT\n  date_trunc('month', CAST(paid_at AS DATE)) AS month,\n  SUM(\n    CASE\n      WHEN order_kind = 'customer_sale' AND status IN ('completed', 'shipped', 'paid') THEN (\n        CAST(line_paid_amount AS DECIMAL(18, 2))\n        - COALESCE(CAST(refund_amount AS DECIMAL(18, 2)), 0)\n      )\n      ELSE 0\n    END\n  ) AS net_revenue\nFROM data\nWHERE CAST(paid_at AS DATE) >= DATE '2026-08-01'\n  AND CAST(paid_at AS DATE) < DATE '2026-10-01'\nGROUP BY 1\nORDER BY 1;\nnpx ava query \"$DATASET_ID\" --dsl @query-finance-months.sql\nnpx ava query \"$DATASET_ID\" --dsl @query-trading-months.sql\n```\n\nYou should see:\n\n| Definition | August (CNY) | September (CNY) | Change (CNY) | Change | \n|---|---|---|---|---|\n| Finance | 4,293,025.42 | 3,697,575.18 | −595,450.24 | −13.87% | \n| Trading | 6,740,687.05 | 7,803,998.06 | +1,063,311.01 | +15.77% | \n\nThe 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)`.\n\nNothing 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.\n\nThe 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:\n\n```\nWITH monthly AS (\n  SELECT\n    selling_store_name,\n    channel,\n    product_name,\n    date_trunc('month', CAST(completed_at AS DATE)) AS month,\n    SUM(\n      CASE\n        WHEN order_kind = 'customer_sale' AND status IN ('completed') THEN (\n          ROUND(CAST(line_paid_amount AS DECIMAL(18, 2)) * CAST(commission_rate AS DECIMAL(6, 4)), 2)\n          - ROUND(COALESCE(CAST(refund_amount AS DECIMAL(18, 2)), 0) * CAST(commission_rate AS DECIMAL(6, 4)), 2)\n        )\n        ELSE 0\n      END\n    ) AS net_revenue\n  FROM data\n  WHERE CAST(completed_at AS DATE) >= DATE '2026-08-01'\n    AND CAST(completed_at AS DATE) < DATE '2026-10-01'\n  GROUP BY 1, 2, 3, 4\n)\nSELECT\n  selling_store_name,\n  channel,\n  product_name,\n  SUM(CASE WHEN month = DATE '2026-08-01' THEN net_revenue ELSE 0 END) AS august_cny,\n  SUM(CASE WHEN month = DATE '2026-09-01' THEN net_revenue ELSE 0 END) AS september_cny,\n  SUM(CASE WHEN month = DATE '2026-09-01' THEN net_revenue ELSE 0 END)\n    - SUM(CASE WHEN month = DATE '2026-08-01' THEN net_revenue ELSE 0 END) AS change_cny\nFROM monthly\nGROUP BY 1, 2, 3\nORDER BY change_cny\nLIMIT 5;\n```\n\nThe 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.\n\nThe file still cannot tell you why those combinations moved. There is no stock or media table to join.\n\n```\nnpx ava dispose \"$DATASET_ID\"\n```\n\nSessions expire after 30 idle minutes. If that happens, load the source again and use the new dataset ID.\n\nIt 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.\n\nIt 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.\n\nThis 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.", "url": "https://wpnews.pro/news/the-same-duckdb-table-two-definitions-two-opposite-trends", "canonical_source": "https://dev.to/yvonneyx/the-same-duckdb-table-two-definitions-two-opposite-trends-3gjd", "published_at": "2026-12-01 17:02:48+00:00", "updated_at": "2026-12-01 17:13:36.463157+00:00", "lang": "en", "topics": ["ai-tools", "ai-agents", "developer-tools"], "entities": ["DuckDB", "AVA", "AntV", "@antv/ava", "@duckdb/node-api", "Node.js"], "also_reported_by": [], "takeaway": "The same DuckDB table produced a 13.87% net-sales decline under the finance definition and a 15.77% increase under the trading definition, so the metric definition must be written explicitly into the SQL rather than inferred from a semantic file.", "recommendation": {"advice": "Write the metric definition explicitly into each SQL statement and cross-check totals with a second independent pass before trusting any trend.", "playbook": "output-verification", "playbook_url": "https://wpnews.pro/playbooks/output-verification"}, "alternates": {"html": "https://wpnews.pro/news/the-same-duckdb-table-two-definitions-two-opposite-trends", "markdown": "https://wpnews.pro/news/the-same-duckdb-table-two-definitions-two-opposite-trends.md", "text": "https://wpnews.pro/news/the-same-duckdb-table-two-definitions-two-opposite-trends.txt", "jsonld": "https://wpnews.pro/news/the-same-duckdb-table-two-definitions-two-opposite-trends.jsonld"}}