Search code examples
kdb

How can I delete a column by index from a kdb table?


For example how would you delete the first column from the following table:

q)t: ([] a: (2018.09.25; 2018.09.25; 2018.09.25); b: `ABC`XYZ`BAC ; c: (10 20 30))
q)t
a          b   c
-----------------
2018.09.25 ABC 10
2018.09.25 XYZ 20
2018.09.25 BAC 30

The expected result:

b   c
---------
ABC 10
XYZ 20
BAC 30

It is possible to use delete a from t but I would like to be able to delete without knowing the exact column name beforehand.


Solution

  • You could use a functional delete:

    q){[t;index]![t;();0b;enlist cols[t]index]}[t;0]
    b   c 
    ------
    ABC 10
    XYZ 20
    BAC 30
    

    https://code.kx.com/q/ref/funsql/#delete

    Use parse in order to see what the q-sql statement looks like in functional form:

    q)parse"delete a from t"
    !
    `t
    ()
    0b
    ,,`a