Search code examples
kdbq-lang

How do I remove multiple columns from a table?


So there's delete col from table to delete a single column. I suppose I could use over to delete multiple columns. But:

  • I'm not sure if this is efficient at all.

  • I'm not quite sure how to use over correctly here. Something like this doesn't work: {delete y from x}/[t;`name`job]


Solution

  • you can delete multiple columns the same way you can select multiple columns.

    delete col1,col2 from table
    

    It would definitely be less efficient to use over in this case.

    There are however examples where you may want to pass column names as symbols into a function that does a select, or delete.

    To do so requires using the functional form of delete: https://code.kx.com/q/ref/funsql/

    Example of functinoal delete

    q)table:([]col1:1 2 3;col2:1 2 3;col3:10 20 30) 
    q)//functional delete
    q){![table;();0b;x]} `col1`col2
    col3
    ----
    10  
    20  
    30  
    q)//inplace functional delete
    q){![`table;();0b;x]} `col1`col2
    `table
    q)table
    col3
    ----
    10  
    20  
    30