benjamin rossman
benjamin rossman

Reputation: 61

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

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

Upvotes: 6

Views: 4801

Answers (2)

Nicole Watterson
Nicole Watterson

Reputation: 51

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

Upvotes: 5

Rahul
Rahul

Reputation: 3969

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

Upvotes: 3

Related Questions