Skip to content
Historical Database

Querying Historical Tables

Historical tables are queried with the same AMI SQL used against realtime tables, with a defined set of rules covering joins, indexes, and predicate forms.

Overview

A historical table answers the same SELECT statements as a realtime table, in the same Center, and can be read alongside live data. A query filtered on a partition column reads only the partitions that can match, so its cost tracks the data it needs rather than the size of the table:

SELECT sym, qty FROM Trades WHERE tradeDate==20260101;

See Partitioning for how that elimination works. The differences from a realtime table are confined to three areas, each covered below: how two historical tables join, how indexes behave, and which predicate forms are accepted.

Joins

Two historical tables can be joined. Both operands are ordinary historical tables, created as in Creating a Historical Table:

CREATE PUBLIC TABLE Trades (
    tradeDate LONG PARTITION,
    sym       STRING,
    qty       LONG
) USE PersistEngine="HISTORICAL";

CREATE PUBLIC TABLE Fills (
    fillDate LONG PARTITION,
    sym      STRING,
    qty      LONG
) USE PersistEngine="HISTORICAL";

INSERT INTO Trades VALUES (20260101,'IBM',100);
INSERT INTO Fills  VALUES (20260101,'IBM',100);

SELECT Trades.qty, Fills.qty
FROM Trades JOIN Fills ON Trades.qty==Fills.qty;

There is no need to stage either side into a temporary table first. The join reads both historical tables directly.

The four-clause rule

A join has to be written in a specific shape. Each clause has one accepted form, and the wrong form fails in a distinct way:

Clause Required form If written the wrong way
SELECT Table-qualified, Table.col. Output columns come back named Table_col —
ON Qualified equality only, Trades.qty==Fills.qty —
WHERE Unqualified, col, not Table.col Unknown namespace variable: Trades.qty
aliases Not supported at all Class not found: Trades, naming the table

So the SELECT and ON clauses qualify their column names with the table, and the WHERE clause must not. Table aliases cannot be used anywhere in the statement.

A column that both tables have, such as qty above, cannot be filtered in the WHERE clause. Unqualified, it fails with Ambiguous column in WHERE clause: qty, and qualified, it is rejected. Filter on a column only one of the tables has, such as tradeDate.

Join limits

  • Two historical tables per join. A third fails with Multiple JOIN statements not supported: JOIN.
  • LEFT JOIN works, but the right-hand table cannot be constrained. A WHERE or ON condition on the right-hand table of a LEFT JOIN is not applied.

Indexes

An index speeds up lookups on a non-partition column:

CREATE INDEX idx_sym ON Trades (sym);

SORT is optional and is the only index type available. There is no PRIMARY or UNIQUE index: an index does not enforce uniqueness, and an indexed column may hold duplicate values. An index is a lookup accelerator, nothing more.

A few rules apply:

  • One index per column, one column per index. A second index on the same column fails with Duplicate Index on column: <name> on <col>. Indexes are single-column; there are no composite indexes.
  • The column must exist. Indexing a column that is not on the table fails with Column not found for table <table>: <col>.
  • A partition column cannot be indexed. It fails with Can not create INDEX on PARTITION column: d. This is not a limitation to work around: partition pruning already does the job an index would, without opening any file. Index the columns you filter on within a partition. See Partitioning.

A new index is held in memory until the table is optimized, which flushes it to disk, one index per partition. See Optimization.

An index does not change query results. A nullable column, or a BOOLEAN column, can be indexed, and MIN() on an indexed column that holds nulls still returns the smallest value that is not null.

Predicate forms

Most WHERE predicates behave as they do against a realtime table. Two cases do not.

A subquery in IN throws. WHERE qty IN (SELECT ...) surfaces Uncaught error: null <-- java.lang.NullPointerException rather than a clean rejection.

There is no LIKE operator. LIKE returns No LIKE operator: use ~~ for a filter match, or =~ for a regex. Use ~~ for a filter match or =~ for a regex.

Every error string above, with its cause and correction, is collected in Troubleshooting.

Aggregates

Standard aggregates work against a historical table, on their own or with GROUP BY:

1
2
3
SELECT sym, SUM(qty), AVG(px), COUNT(*), MIN(qty), MAX(qty)
FROM Trades
GROUP BY sym;

Aggregate output columns are named <FUNCTION>_<column> — SUM_qty, AVG_px, MIN_qty — and COUNT(*) comes back as COUNT_.

To count distinct values, use countUnique, which ignores nulls. COUNT_UNIQUE does not exist (No such method: COUNT_UNIQUE(long)).

Note

Aggregate results come back as rows through an AI client's MCP tools too. See Operating the Historical Database over MCP.