q cheat sheet
The operators the q problems lean on. Run them in a q session to see the exact output.
| Task | q | Note |
|---|---|---|
| Running total | sums x | sums 1 2 3 gives 1 3 6 |
| Consecutive differences | deltas x | the first item is kept as is |
| Moving average | 3 mavg x | early items use what is available |
| Round down to a bucket | 5 xbar x | 5 xbar 7 12 gives 5 10 |
| Unique values | distinct x | keeps first-seen order |
| Sort | asc x / desc x | |
| Count per value | count each group x | returns a dict |
| Weighted average | w wavg x | weights go on the left |
| Group and aggregate | select avg price by sym from t | result is keyed by sym |
| Filter by group aggregate | select from t where size=(max;size) fby sym | rows with the max size per sym |
| As-of join | aj[`sym`time; trade; quote] | the last key column is the time |
| Window join | wj[w; `sym`time; t; (q; (max;`bid))] | aggregate over a time window |
| Apply over a list | f each x | |
| Fold and scan | +/ x and +\ x | over folds, scan keeps each step |