Search code examples
kdb

Why does 'sum' work but '+/' not work in KDB queries?


Why does sum work here, but the underlying form +/ not work? (Taken from https://code.kx.com/q/ref/sum/)

t: ([]name:`Jack`Jill`Janet;hair:`brown`black`fair;eye:`blue`green`hazel;age:12 9 14)

q)select sum age from t
age
---
35
q)select +/age from j
'/
  [0]  select +/age from j
               ^

Solution

  • This is because +/ is k syntax. To invoke it (and similar k constructs) in q you will need to wrap it in parentheses.

    select enlist (+/)age from j
    

    In general, if an inbuilt q keyword exists for the associated k expression, you should use the keyword (sum in this case) as it likely carries further optimisations. In the case of sum q will automatically enlist the result inside a select statement which (+/) won't do. Hence why I have done it manually above. Otherwise expect a 'rank error.