英文:
Is there a way to do wj with aggration on columns from the left table?
问题
我有以下代码。`weight` 来自右表,聚合有效。`weightLeft` 来自左表,聚合无效。有可能让 `weightLeft` 有效吗?
t:([]sym:3#`ibm;time:10:01:01 10:01:04 10:01:08;price:200 201 205;weightLeft:3 4 5);
price:101 103 103 104 104 107 108 107 108;
weight:10 20 30 10 20 30 10 20 30;
q:([]sym:ibm; time:10:01:01+til 9; price:price; weight:weight);
sym`time;
f:
w:-2 1+:t.time;
wj[w;f;t;(q;(sum;price);({sum x*y};
price;weight))]; // OK
price);({sum x*y};
wj[w;f;t;(q;(sum;price;
weightLeft))]; // 失败
<details>
<summary>英文:</summary>
I have below code. `weight` is from the right table, and the aggregation works. `weightLeft` is from the left table, and the aggregation doesn't work. Is it possible to make `weightLeft` works?
t:([]sym:3#`ibm;time:10:01:01 10:01:04 10:01:08;price:200 201 205;weightLeft:3 4 5);
price:101 103 103 104 104 107 108 107 108;
weight:10 20 30 10 20 30 10 20 30;
q:([]sym:ibm; time:10:01:01+til 9; price:price; weight:weight);
sym`time;
f:
w:-2 1+:t.time;
wj[w;f;t;(q;(sum;price);({sum x*y};
price;weight))]; // OK
price);({sum x*y};
wj[w;f;t;(q;(sum;price;
weightLeft))]; // failed
</details>
# 答案1
**得分**: 1
我认为无法以这种方式使用左表的列。如果您使用身份运算符`::`作为聚合函数,您可以看到kdb将该列解释为null符号,因此出现类型错误。
q)wj[w;f;t;(q;(sum;price);(::;
weightLeft))]
符号 时间 价格 重量左
ibm 10:01:01 204 ``
ibm 10:01:04 414
ibm 10:01:08 430
对于您的用例,您可以在`price`上进行聚合,然后在之后乘以`weightLeft`吗?
q)update price*weightLeft from wj[w;f;t;(q;(sum;`price))]
符号 时间 价格 重量左
ibm 10:01:01 612 3
ibm 10:01:04 1656 4
ibm 10:01:08 2150 5
<details>
<summary>英文:</summary>
I do not think it is possible to use columns from the left table in that manner. If you have use the identity operator `::` as your aggregation function you can see how kdb interprets that column as a null symbol, hence the type error.
q)wj[w;f;t;(q;(sum;price);(::;
weightLeft))]
sym time price weightLeft
ibm 10:01:01 204 ``
ibm 10:01:04 414
ibm 10:01:08 430
For your use case could you aggregate on `price` then multiply by `weightLeft` afterwards?
q)update price*weightLeft from wj[w;f;t;(q;(sum;`price))]
sym time price weightLeft
ibm 10:01:01 612 3
ibm 10:01:04 1656 4
ibm 10:01:08 2150 5
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论