r/SQL • • 4d ago

SQLite Where Boundaries Break: The Metric With Two Definitions

Start with the metric that has two plausible definitions, because that's where the boundary actually gets tested. Average order value is a good one: revenue divided by orders, or revenue divided by distinct customers, and does the numerator net out refunds or not. Both are defensible. Neither is "the" answer until someone writes it down.

That's the real dividing line. Not who can type a join, but whether the number has one owner and one definition that both the SQL and the English resolve to.

The setup I'd argue for has three tiers, and each tier only holds up if the one below it is in place. Postgres syntax below, but the shape is the same anywhere.

**Tier 1: plain-English answers for the business.** Safe only if the language layer reads from a metric store, not from raw schema. A definitions table is enough to start:

```sql
create table metrics.definitions (
metric_name varchar,
owner varchar,
sql_body varchar,
grain varchar,
updated_at timestamp
);

insert into metrics.definitions values (
'average_order_value',
'finance',
'sum(net_revenue) / count(distinct order_id)',
'order',
current_timestamp
);
```

If a question can't be answered from a row in that table, the honest reply is "we don't have that metric yet," not a confident guess. That one rule kills most of the drift.

**Tier 2: read-only SQL for analysts.** They need the grain and the filters documented, not just the tables. Column names lie. `status` on an orders table might mean fulfilment state or payment state, and whoever writes the query has to know which. Write it down once instead of re-explaining it in every thread.

**Tier 3: engineers on base tables.** Full access, because they maintain the definitions and rebuild them when the source schema moves. Tiers 2 and 3 cost real time to run, but that's the cost of keeping tier 1 honest.

Where it falls apart, in my opinion, is skipping straight from "we have tables" to "we have a chat box." Natural language over raw schema doesn't fail loudly. It answers confidently using a join you never intended, and the person asking has no way to tell.

So, for anyone here running a semantic or metric layer: who owns the definitions, the data team, finance, or the business unit? And has that ownership held, or does the metric drift back to whoever asks loudest?

0 Upvotes

8 comments sorted by

11

u/Zestyclose-Turn-3576 4d ago

The "written by AI" style of this is very offputting

2

u/baubleglue 4d ago

How revenue/nun of customer can be 'avarage_order_value'? And how is grain become 'order'?

1

u/Muted_Jellyfish_6784 3d ago

Ownership is the dividing line, and versioning is the practical piece. Store each definition with its owner, effective date, and exact SQL, and when it changes keep the old version so historical reports still reproduce. Name variants explicitly (aov_gross, aov_net_of_refunds) instead of letting one name mean two things.
Then make every answer say which version it used. We run a decision intelligence platform that ties each number to its definition and the source rows behind it, so a disagreement becomes a check of which definition was used rather than a debate over whose query is right.

1

u/Discordbothost 2d ago

Add a versioned test dataset with approved expected totals beside each metric definition. Run it when the SQL or source joins change. An owner and a formula won't catch a join multiplying revenue while the distinct order count stays unchanged.

-1

u/Aggressive_Ad_5454 4d ago

Indeed. When we wring wisdom out of raw data, we need to understand pretty clearly what the raw data means and what wisdom we want.

It’s often helpful to talk to somebody who knows how to talk in accounting jargon in order to describe the wisdom. “Average sales per customer net of returns”, for example, describes a particular metric. And the trend of that metric might be helpful. Or confusing, if the business just acquired a whole mess of new small customers.

-1

u/WorldOfUmbro 4d ago

Weer using metric views on Databricks. All our agents (Genie Agents, custom agents) and our dashboards use the same definitions. No more different answer in different source. This helped us scale our Genie Agents without any issues.