How to select a subset of columns from table with list in kdb+/q?
Asked Answered
kdb
M

2

6

Given the following table:

time     | col1  col2  col3  ...
--------------------------------
10:53:02 | 89    89    76    ...
...

How does one select a subset of columns from this table (including the index) referenced by a list of column names i.e. cols:('col1';'col3'); Whereby the expected result would be:

time     | col1  col3
----------------------
10:53:02 | 89    89   
...

Thanks

Meathead answered 3/9, 2019 at 21:46 Comment(1)
Just to point out, cols is not a valid variable name in KDB+, as it is already the name of a built-in function. Usually a lower case c will suffice. In the above, cols:('col1';'col3') would be better written as c:`col1`col2Thermosphere
L
5

You could use a take (#) keyword with the each right (/:) adverb. So q will take the subset of sym and price columns from the table t and return a table with your key and your required subset of data

q)t:([time:.z.z+ 1 2];sym:`a`b;price:10 20;vol:30 40)
q)c:`sym`price
q)c#/:t
time                   | sym price
-----------------------| ---------
2019.09.05T07:56:36.069| a   10
2019.09.06T07:56:36.069| b   20
Lessor answered 4/9, 2019 at 8:57 Comment(0)
L
3

There are couple of ways to do this:

q) t:([time:.z.z+ 1 2];sym:`a`b;price:10 20;vol:30 40)

Columns required in output:

q) c:`sym`price  

Add table key columns to above list:

q) c:keys[t],c

Using Functional Select:

q) keys[t] xkey ?[`t;();0b;c!c]

Using Take(#) operator

q) keys[t] xkey c#0!t

Output:

time                   | sym price
-----------------------| ---------
2019.09.04T23:05:21.577| a   10
2019.09.05T23:05:21.577| b   20
Loyola answered 3/9, 2019 at 23:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.