1

私はaqテーブルを持っています。キーが設定されていない列の数は可変です。また、これらの列名には、名前に整数が含まれています。実際の名前を実際に使用せずに、これらの列に対していくつかの機能を実行したい

どうすればこれを達成できますか?

例えば:

 table:   

 a |  col10  col20 col30 

 1 |    2      3     4
 2 |    5      7     8

 // Assume that I have numbers 10, 20 ,30 obtained from column names

    I want something like **update NewCol:10*col10+20*col20+30*col30 from table**

     except that no.of columns is not fixed so are their inlcluded numbers
4

4 に答える 4

7

機能更新を使用したい (ここに示す簡単な例: http://www.timestored.com/kdb-guides/functional-queries-dynamic-sql#functional-update )

この特定のクエリでは、select 句の計算ツリー、つまり機能更新ステートメントの最後の部分を生成したいと考えています。これを行う最も簡単な方法は、同様のステートメントを解析してから、その形式を再作成することです。

q)/ create our table
q)t:([] c10:1 2 3; c20:10 20 30; c30:7 8 9; c40:0.1*4 5 6)
q)t
c10 c20 c30 c40
---------------
1   10  7   0.4
2   20  8   0.5
3   30  9   0.6

q)parse "update r:(10*c10)+(20*col20)+(30*col30) from t"
!
`t
()
0b
(,`r)!,(+;(*;10;`c10);(+;(*;20;`col20);(*;30;`col30)))
q)/ notice the last value, the parse tree
q)/ we want to recreate that using code
q){(*;x;`$"c",string x)} 10
*
10
`c10
q){(+;x;y)} over {(*;x;`$"c",string x)} each 10 20
+
(*;10;`c10)
(*;20;`c20)
q)makeTree:{{(+;x;y)} over {(*;x;`$"c",string x)} each x}

/ now write as functional update
q)![t;();0b; enlist[`res]!enlist makeTree 10 20 30]
c10 c20 c30 c40 res
-------------------
1   10  7   0.4 420
2   20  8   0.5 660
3   30  9   0.6 900

q)update r:(10*c10)+(20*c20)+(30*c30) from t
c10 c20 c30 c40 r
-------------------
1   10  7   0.4 420
2   20  8   0.5 660
3   30  9   0.6 900
于 2014-06-18T14:54:22.020 に答える
1

これも高速な別のソリューションです。

t,'([]res:(+/)("I"$(string tcols) inter\: .Q.n) *' (value t) tcols:(cols t) except  keys t)

時間をかけて、単語数を減らすこともできます。ロジックは次のようになります。

a:"I"$(string tcols) inter\: .Q.n

ここでは、最初に列名から整数を抽出し、それらをベクトルに格納しています。変数 'tcols' は、キー列を除くテーブルの列に過ぎないクエリの最後で宣言されています。

b:(value t) tcols:(cols t) except keys t

ここでは、各列ベクトルを抽出しています。

c:(+/) a *' b

各列ベクトル (var b) をその整数 (var a) で乗算し、結果の各リストから対応する値を追加します。

t,'([]res:c)

最後に結果を一時テーブルに保存し、それを t に結合します。

于 2014-06-18T20:11:04.123 に答える