Skip to content

Combine tables

Every wire into a node is a named table, named by its handle. Nothing merges several wires on its own: the node’s formula says how, with three tools.

Tool What it does
inputs().prop(handle) The table a wire carries, read-only
table.find(column, value), table.findAll(column, value), table.rows() A lookup: the first matching row, every matching row, or all rows
appendRows(table) Adds every row of a table, with its styles, to the node’s rows

The first wire into a node has the handle input; each further wire takes a handle from its source node’s title, which you can rename on the wire’s Overview panel. inputs("prices") is a short form of inputs().prop("prices"). See Wires and named tables for how wires and handles work on the canvas.

Wires into the node The first statement’s rows Named tables
None Empty None
One That wire’s rows That wire, on its handle
Several Empty Every wire, on its own handle

A node with several wires and no statement that reads them fails with a message naming the missing merge formula, so a missing merge is visible rather than silent.

setProp("price", inputs().prop("prices").find("ISIN", prop("ISIN")).prop("price"))
  • find(column, value) returns the first row, in table order, whose column equals the value, or empty when none matches.
  • findAll(column, value) returns every matching row as a list, and rows() every row.
  • row.prop(name) reads a field of a table row. The value keeps the data styles of the cell it came from.
  • A miss writes an empty cell. To write something else on a miss, test the match first:
setProp("price", let(
hit, inputs().prop("prices").find("ISIN", prop("ISIN")),
if(empty(hit), -1, hit.prop("price"))
))

A let binding lives inside its statement, so each column you take from a lookup reads the table in its own setProp.

Lists of matching rows work with the list functions:

setProp("open_orders", inputs().prop("orders").findAll("account", prop("account")).count())
setProp("open_amount", inputs().prop("orders").findAll("account", prop("account")).map(current.prop("amount")).sum())
appendRows(inputs().prop("trades_eu"))
appendRows(inputs().prop("trades_us"))

The columns afterwards are the union of both tables; a column only one side has reads empty on the other side’s rows.

appendRows(inputs().prop("trades"))
setProp("price", inputs().prop("prices").find("ISIN", prop("ISIN")).prop("price"))
setProp("priceDate", inputs().prop("prices").find("ISIN", prop("ISIN")).prop("asOf"))

Enrich without overwriting a value the row has

Section titled “Enrich without overwriting a value the row has”
appendRows(inputs().prop("trades"))
if(
empty(prop("currency")),
setProp("currency", inputs().prop("reference").find("ISIN", prop("ISIN")).prop("currency"))
)
appendRows(inputs().prop("left"))
appendRows(inputs().prop("right"))
groupBy(prop("id"))
setProp("id", prop("id").first())
setProp("amount", prop("amount").filter(not(empty(current))).first())

Both sides stack, then the key collapses them into one row per id. A key only one side has still gets its row, and where both sides carry a value the left one wins because it was appended first.

appendRows(inputs().prop("a"))
setProp("amount", inputs().prop("b").rows().at(rowIndex()).prop("amount"))

at() past the end is empty, so a shorter b leaves amount empty on the trailing rows.

appendRows(inputs().prop("a"))
appendRows(inputs().prop("b"))
groupBy(prop("ISIN"))
setProp("ISIN", prop("ISIN").first())
setProp("qty", prop("qty").sum())
  • A named table is read-only: statements write to the node’s rows, so sortBy and groupBy apply to the rows, not to a table.
  • appendRows takes a table, never a filtered list of rows, and stands on its own line, never inside if, do, or each.
  • An appended row has no input row behind it, so an exception raised on it is filed against the output row, and its lineage points to the table row on the wire it came from.
  • A table whose columns are unknown, because no schema describes the wire and no rows have loaded yet, accepts any column name; a column it turns out not to have matches nothing.

When a join is easier to say in SQL, fromQuery reads named tables by their handles with .tables(…):

fromQuery(`SELECT t.*, p.price
FROM "trades" t LEFT JOIN "prices" p ON p.isin = t.isin`, "sql")
.tables("trades", "prices")

A query keeps no per-cell lineage or data styles. See Templates, queries, and scripts.