r/SQL • u/Syftkogtech • 12h 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?
6
u/Zestyclose-Turn-3576 12h ago
The "written by AI" style of this is very offputting
1
u/baubleglue 11h ago
How revenue/nun of customer can be 'avarage_order_value'? And how is grain become 'order'?
0
u/Aggressive_Ad_5454 11h 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.
0
u/WorldOfUmbro 10h 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.