In Memory Tables · Updating Tables · Practice Question
Add a Boolean Flag Column from a Condition
Question
Given a table t with a price column, update the table to add a new column d, which takes the value 1b when price > 150.0, and 0b otherwise.
For example:
t:([] sym:`A`B`C; price:120.5 151.2 175.0)Solution
update d:price>150.0 from `tAlternative Solution
update d:1b from `t where price>150.0Explanation
price>150.0 is a comparison applied across the whole price column at once, producing a boolean for every row — update d:price>150.0 from tsimply stores that boolean column asd, covering every row of t` in a single pass.
The alternative takes a different route: update d:1b from t where price>150.0only touches the rows matching thewhereclause, settingdto1bon those rows specifically. Whenupdatecreates a new column for only some rows, the other rows are filled with the type's null value. Booleans have no null, so those rows get0b — which gives the required result here. For other types this matters: ``update d:high from t where price>150.0`` would leave the remaining rows as the null symbol `` ``. The first solution is simpler, since the comparison itself produces both 0b and 1b in one expression.