Skip to content

Commit e30d2a9

Browse files
authored
Merge pull request #137 from defog-ai/trade-all-ids
Explain EU trade series without a trade token in the SQL guide
2 parents 1e500bd + 8c195bf commit e30d2a9

1 file changed

Lines changed: 60 additions & 6 deletions

File tree

‎references/data/sql-guide.md‎

Lines changed: 60 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -266,16 +266,16 @@ Use these rules:
266266
first and last observed dates.
267267
- Build exact series IDs and join them to `data_points`; an exact
268268
`series_id` match avoids the pattern scan. The detailed ID shape is
269-
`eu_comext_{M|X}_{reporter}_{partner}_{te|ti|tl|tm}_p1_cn8_{product_code}_{eur|kg|su}`.
269+
`eu_comext_{M|X}_{reporter}_{partner}_{te|ti|tl|tm|all}_p1_cn8_{product_code}_{eur|kg|su}`.
270270
`M` means imports and `X` exports. Reporter and partner are lowercase ISO2
271271
codes, except that `xi` is Eurostat's code for Northern Ireland and `xu`
272272
its code for the United Kingdom without Northern Ireland. The stored trade
273273
token follows the partner's status in each month: `te` outside the EU, `ti`
274-
inside the EU, `tl` for `xi` and `tm` for `xu` (both from 2021). A range
275-
that crosses an EU accession or the United Kingdom's February 2020 exit must
276-
query both exact series with non-overlapping date bounds. The `gb` series
277-
is the whole United Kingdom in every month. From 2021, `gb` equals `xu`
278-
plus `xi`, so never add `xi` or `xu` to `gb`.
274+
inside the EU, `tl` for `xi` and `tm` for `xu` (both from 2021). For a
275+
range that crosses a partner's EU entry or exit, write `all` in place of
276+
the token; see **Series without a trade token (`all`)** below. The `gb`
277+
series is the whole United Kingdom in every month. From 2021, `gb` equals
278+
`xu` plus `xi`, so never add `xi` or `xu` to `gb`.
279279
- Use `_eur` for trade value, `_kg` for weight, and `_su` for supplementary
280280
quantity. Never sum these metrics together. Supplementary units are
281281
product-specific, so return `series.measurement_units` with an exact CN8
@@ -291,6 +291,60 @@ Use these rules:
291291
combine the results locally. Every branch must use the exact-ID join, never a
292292
`series_id` pattern.
293293

294+
#### Series without a trade token (`all`)
295+
296+
Eurostat stores trade with a partner under `ti` while the partner is in the
297+
EU and under `te` while it is outside. A partner that joined or left the EU
298+
therefore has its months in two stored series. The United Kingdom changes
299+
from `ti` to `te` in February 2020, and Croatia from `te` to `ti` in July
300+
2013. Write `all` in place of the token to read one series that holds the
301+
months of every token, with no date split:
302+
303+
```sql
304+
SELECT extract(year FROM time)::int AS year,
305+
count(*) AS months,
306+
SUM(value) AS value_eur
307+
FROM data_points
308+
WHERE series_id = 'eu_comext_M_de_gb_all_p1_cn6_total_eur'
309+
AND time >= DATE '2018-01-01' AND time < DATE '2023-01-01'
310+
GROUP BY 1
311+
ORDER BY 1;
312+
```
313+
314+
Run with `schema="eu_comext_de"`, this tested example returns 12 months for
315+
each year from 2018 to 2022: the `ti` months up to January 2020 and the `te`
316+
months after it.
317+
318+
Rules for `all`:
319+
320+
- `all` works in `data_points`, `series` and `dimensions`, in exact IDs, in
321+
`IN` lists and in constructed-ID joins
322+
(`'eu_comext_M_de_gb_all_p1_cn8_' || lower(p.product_code) || '_eur'`).
323+
The exact-ID rule above still applies.
324+
- In one statement, write every Comext ID with `all` or every ID with its
325+
stored token. The server refuses a statement that mixes them, or that
326+
names a stored token in other text (`split_part(series_id, '_', 6) = 'te'`
327+
or `'%_te_%'`).
328+
- With `all`, use `series_id` only as it is: select, group, order or count
329+
it, compare it with `=`, `<>` or `IN`, or take a part other than the sixth
330+
with `split_part(series_id, '_', n)`. The server refuses functions of
331+
`series_id` (`lower`, `substring`, `replace`, regular expressions) and a
332+
filter on the `trade_type` dimension.
333+
- An `all` series never adds two tokens. If two tokens hold a value for the
334+
same month, the read fails and names the series and the month.
335+
- A few `_su` products are in one unit under one token and in another unit
336+
under the other. For example, France's imports of MDF board (CN8
337+
44111210) from the United Kingdom are in square metres under `ti` and in
338+
cubic metres under `te`. These parts cannot be added. An `all` ID for
339+
such a product fails when the statement names it with `=` or `IN`. A
340+
constructed ID leaves it out and returns the other rows, and the result's
341+
`left_out_series_note` names each product left out, its two units and the
342+
stored IDs that give each unit. Read those stored series separately, one
343+
unit each, and say in the answer which products are not in the total.
344+
- `all` covers `te`, `ti`, `tl` and `tm`. Cyprus also files part of its
345+
imports from `xu` in January to March 2021 under a fifth token, `tk`,
346+
which `all` does not include. Use `gb` for Cyprus's United Kingdom totals.
347+
294348
To inspect product mappings, qualify the shared table from any country query:
295349

296350
```sql

0 commit comments

Comments
 (0)