qSQL · Functional Form · Practice Question
Functional Select With Aggregations
Question
Given the following table:
t:([] sym:`AAPL`MSFT`GOOG`TSLA`NVDA; b:10 20 15 30 25)Write the functional equivalent of:
select minB:min b, avgB:avg b from tExpected result:
minB avgB
---------
10 20Solution
?[t; (); 0b; `minB`avgB!((min;`b);(avg;`b))]Explanation
The query computes two aggregations across the whole table — no grouping, no filtering. Mapping that to the four-argument functional form:
| Position | Value | Meaning |
|---|---|---|
| table | t | source table |
| where | () | no filter |
| by | 0b | no grouping |
| select | `minB`avgB!((min;`b);(avg;`b)) | two computed columns |
When the output column is a computation rather than a plain pass-through, the dictionary value is a small list — a parse tree — describing the operation: (function; column).
(min;`b) / apply min to column b → minB
(avg;`b) / apply avg to column b → avgBThe full select dictionary:
`minB`avgB!((min;`b);(avg;`b))
/ minB -> min b
/ avgB -> avg bVerifying with parse
If you are ever unsure of the functional form for a particular expression, parse will reveal it. Pass your qSQL as a string and read back the structure q would use internally:
parse "select minB:min b, avgB:avg b from t"?
`t
()
0b
`minB`avgB!((min;`b);(avg;`b))Each line is one of the four arguments, in order. The last line matches the dictionary in the solution exactly.
A few things to watch when reading parse output: it uses q's underlying k notation, so built-in functions may appear with unfamiliar names, and symbol values in conditions will have a leading , (k's way of writing enlist) that you need to replace when writing the functional form yourself.