2017-10-15 123 views
0

因此有delete col from table删除单个列。我想我可以使用over来删除多列。但是:如何从表中删除多列?

  • 我不确定这是否有效。

  • 我不太清楚如何在这里正确使用。像这样的东西不起作用:{delete y from x}/[t;`name`job]

回答

2

可以删除多个列,您可以选择多列的方式相同。

delete col1,col2 from table 

在这种情况下使用它肯定效率较低。

然而,您可能想要将列名作为符号传递给执行选择或删除的函数。

这样做需要使用删除功能形式:https://code.kx.com/q/ref/funsql/

functinoal的例子删除

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 
+0

感谢,功能形式是一个我需要什么。 – rgrinberg

+0

嗯,功能删除似乎不工作时列的列表是空的。我希望不要碰到表格,但它会删除它中的所有行。 – rgrinberg

0

我不能低于etc211的解决方案发表意见,所以我刚开始另一个答案后。

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. 

对于上面,为什么你不创建一个函数,选择你愿意删除的列?

让我们假设表t的你包含列名:col1,col2,col3,col4 和要删除:col5,col6

从Q码:

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

上面会首先检查列的存在,你想要删除;如果存在目标列删除目标,它将删除这些列。

1

对于在内存中的表,你也可以使用降: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 
1

对于在内存中的表,你也可以使用降: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