Queries
Query verbs transform data. Use |> to pass the result on to the next verb.
Try filter, select, and sort, then aggregate or group for summaries, and reach for lookup to bring a value across from another table, embed to join multiple tables and reshape to change table shape.
Pipelines
A short query can stay on one line:
Name
Alice Bob
For a longer query, put each next step on an indented line beginning with |>.
Name
Alice Bob
Select
Keeps columns from each row and can calculate new ones. Write a column name to keep it, or New = expression to add a calculated column.
Name
Alice Bob
Doubled Tripled
60 90
50 75
D
50
60
Later calculations in the same select may use columns created earlier. The next query verb sees only the columns produced by the select.
A parenthesized expression over one column, such as (Age |> (format.number 0 1)) or (Name |> bold), keeps that column's name without repeating Age = ... on the left.
Name Age
Alice 30 Bob 25
To keep every column except some, name the ones to drop with a leading -:
Name
Alice Bob
Extend
Keeps every column and adds calculated ones. A name the table already has replaces that column in place; a new name goes at the end.
Name Age Doubled Grown
Alice 30 60 true Bob 25 50 false
Name Age
Alice 31 Bob 26
Name Age
Alice 30 Bob 25
Use extend to add a step to a pipeline without listing the columns again, and select to choose and order the columns a reader sees.
Filter
Keeps the rows whose condition is true. Combine conditions with and, or, and not.
Name Age
Alice 30
Name Age
Bob 25
Name Age
Alice 30
Name Age
Bob 25
!= tests inequality, and in tests membership in a list.
Name Age
Bob 25
Name Age
Alice 30
Map
Transforms each input element and returns a list.
["ALICE" "BOB"]
Sort
Orders rows by one or more columns. Put - before a column for descending order.
Name Age
Bob 25 Alice 30
Name Age
Alice 30 Bob 25
Dept Name Age
Eng Alice 30 Eng Bob 25 Eng Dan 25 Sales Cara 30
Distinct
Removes duplicates while keeping the first occurrence.
Dept Name
Eng Alice Sales Bob Eng Cara
Reverse
Reverses the order of a table.
Name Age
Bob 25
Alice 30
Aggregate
Returns one summary value for the entire table.
27.5
2
Available summaries are sum, count, count-distinct, avg, min, max, product, all, any, variance, and stddev; all ignore null cells. sum, product, count, and count-distinct return plain 0 or 1 when a group has no non-null values; the others return null.
Most summaries name a column, such as avg Age, min Age, or count-distinct Dept. Bare count counts rows, count Age counts the rows where Age is not null, and count (Age > 30) counts the rows where the condition holds.
1
Group
Makes one result row for each distinct group and calculates summaries within it. The result is another table, so it can continue through a pipeline.
Dept Total Oldest
Eng 80 30
Sales 30 30
Dept N
Eng 3
Sales 1
A summary may be used inside an expression, such as a format or arithmetic over it. The expression runs on the group's result.
Dept Mean Oldest
Eng 26.666666666666667 30.0
Sales 30 30.0
When group has no summaries, it places each group’s remaining rows in an inner table:
Dept inner
Eng (Alice 30 Bob 25 Dan 25)
Sales (Cara 30)
Running totals
running keeps every row and adds a column of a summary over the rows so far, in the table's order. Name columns before the summary to restart it where they change, as a balance restarts per account.
Account Day Amount Balance
A 1 10 10 A 2 -4 6 B 1 5 11 A 3 2 13
Account Day Amount Balance
A 1 10 10 A 2 -4 6 B 1 5 5 A 3 2 8
The summaries are sum, count, min, max and avg. A name the row already has is replaced in place, so an opening balance adds on afterwards: |> running Balance = (sum Amount) |> extend Balance = (opening + Balance).
Flatten
Expands grouped inner table back into ordinary rows.
Dept Name Age
Eng Alice 30
Eng Bob 25
Eng Dan 25
Sales Cara 30
Dept Name Age
Eng Alice 30
Sales Cara 30
Embed
Matches rows from two tables. Each left row gains an inner table containing the right rows with the same key; unmatched left rows keep an empty inner table.
Dept Lead inner
Eng Alice (X Y) Sales Bob (Z W) Ops Charlie ()
Dept Lead Headcount
Eng Alice 2 Sales Bob 2 Ops Charlie 0
Apply flatten when you want one ordinary row for each match:
Dept Lead Name
Eng Alice X
Eng Alice Y
Sales Bob Z
Sales Bob W
Use a condition such as Dept == Region when the key columns have different names. Add more conditions to match on several columns.
Lookup
Brings one matching row's columns across, for when each left row needs a value from the other table. The new columns are optional: a left row with no match keeps its place and gets null, and or-else gives it a value. When several right rows match, the first one in the right table counts.
Dept Name Lead
Eng X Alice Eng Y Alice Sales Z Sales W
Name Lead
X Alice Y Alice Z Unassigned W Unassigned
Keys work as in embed. A column the left table already has cannot come across; drop or rename it first.
Reshape
Moves data between tall row-oriented tables and wide column-oriented layouts.
Dept Jan Feb Mar
Eng 19 12
Sales 7 8
Dept Month Amount
Eng Jan 19
Eng Feb 12
Sales Jan 7
Sales Feb 8
Month.. turns the distinct month values into columns and summarizes duplicate cells with sum. The reverse form names the wide columns in a list and stacks them back into Month and Amount columns. Missing combinations become null.
Where next
• doc/chart.ki — turn a query result into a chart