Search code examples
kdb

Understanding a q-sql query with a sublist


items sales prices
------------------
nut   6     10
bolt  8     20
cam   0     15
cog   3     20

q)select {x}[prices] sublist' prices from tab

I am just getting into kdb q-sql and I saw some legacy code which is similar to the above. I was wondering if someone could explain the meaning behind this query. Especially the usage of the '


Solution

  • The query here is applying sublist - which is a function that will return a sublist of x elements in y.

    The ' in this statement specifies that the function is applied to each element in the list.

    I believe this can explained with a simple example. If we take two lists:

    x:1 2 3
    y:(1 2 3 4;5 6 7 8;10 11 12)
    

    And I apply sublist' as follows:

    q)x sublist' y
    ,1
    5 6
    10 11 12
    

    The first element of x has been applied to the first level of y. And so on.