2017-10-15 140 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