Search code examples
kdb+

select column from a table based on a variable name in q kdb as a list


I have a table like :

A_1 A_2 A_3 
1   2    3 
4   5    6
7   8    9 

I want to select a column from a table as list where the name of coulmn will be variable something like :

x:1 
exec `$"A_", string x from table 
expected output : 1 4 7 

i know that i can do it by

--
enlist[`$"A_", string x]#table 
--

but i will have a table and

--
value  enlist[`$"A_", string x]#table  
--

doesnt work


Solution

  • You index using the symbol to get the vector:

    q)t:([] A_1:1 4 7;A_2:2 5 8;A_3:3 6 9)
    q)x:1
    q)t`A_1
    1 4 7
    q)t`$"A_",string x
    1 4 7
    

    https://code.kx.com/q/kb/faq/#column-indexing