有没有一种方法可以在左表的列上进行带聚合的wj(join)?

huangapple go评论51阅读模式
英文:

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);
f:
sym`time;
w:-2 1+:t.time;

wj[w;f;t;(q;(sum;price);({sum x*y};price;weight))]; // OK
wj[w;f;t;(q;(sum;
price);({sum x*y};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&#39;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);
f:
sym`time;
w:-2 1+:t.time;

wj[w;f;t;(q;(sum;price);({sum x*y};price;weight))]; // OK
wj[w;f;t;(q;(sum;
price);({sum x*y};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>



huangapple
  • 本文由 发表于 2023年4月19日 17:07:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76052680.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定