0

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]

rgrinberg
  • 9,638
  • 7
  • 27
  • 44

3 Answers3

3

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 
emc211
  • 1,369
  • 7
  • 14
  • Thanks, the functional form is exactly what I needed. – rgrinberg Oct 15 '17 at 19:36
  • Hmm, functional delete doesn't seem to work when the list of columns is empty. I'd expect that not to touch the table at all, and yet it deletes all the rows in it instead. – rgrinberg Oct 15 '17 at 19:50
3

For an in-memory table you can also use drop: http://code.kx.com/q/ref/lists/#_-drop

q)((),`col1)_table
col2 col3
---------
1    10
2    20
3    30
q)((),`col1`col3)_table
col2
----
1
2
3
q)((),`)_table
col1 col2 col3
--------------
1    1    10
2    2    20
3    3    30
terrylynch
  • 11,844
  • 13
  • 21
0

i cannot comment below etc211's solution, so i just started another answer post.

Hmm, functional delete doesn't seem to work when the list of columns is empty. I'd expect that not to touch the table at all, and yet it deletes all the rows in it instead. 

For above, why don't you create a function that selects the columns that you are willing to delete?

let's assume the table t of yours contain column names:col1,col2,col3,col4 and you want to delete: col5,col6

from q code:

tgt_cols:`col5`col6;
filtered_cols: (cols t) inter tgt_cols;
if[0 < count filtered_cols;
    {![`t;();0b;x]} filtered_cols];

Above will first check the existence of the columns that you want to remove; and if the target-columns-to-delete exists, it will remove those columns.

jeonw
  • 123
  • 8