根据比较 kdb+ 表中特定订单 ID 的最后两行来创建新列

问题描述 投票:0回答:1

存在一个表,其中包含许多唯一 orderID 条目(即唯一 orderID

123
可以有 N 个条目)。我想比较我拥有的每个唯一 orderID 的最后 2 个条目,并根据在每个 orderID 的 2 个条目之间比较值时返回 true 的条件向结果集中添加一个新列。

我该怎么做?这是我到目前为止所尝试的,但在第二行代码中不断遇到长度错误:

myResult:select from myTable where ({x in -2#x;i) fby orderID; / this works by getting the last 2 entries for each unique orderID

/ trying to check if the second row's myType is a certain value, and if my first entry for myValue is equal to "0", and if the start time of my second row - the end time of my previous row is greater than a certain number of seconds
update didEventHappen:?[(next[myType] in `TYPE_A) and (myValue="0") and ((next[start.time] - end.time) > 00:00:01.000); 1b; 0b] fby orderID from myResult 
kdb kdb+ qsql
1个回答
0
投票

如果没有数据样本,很难知道,但在我看来,你的问题可能是:

  1. 对于不使用

    fby
    的更新语句,您使用
    by
    。您在 where 子句中使用
    fby

  2. 您的 myValue 列是字符类型(“c”)还是字符串类型(“C”)?如果它是字符串,那么您的等式将无法正常工作,但 raze 可以避免歧义。

所以这应该可行(暂时忽略时间戳子句,您可以将其添加到其中):

myTable:([]orderID:100?til 10;myType:100?`TYPE_A`TYPE_B;myvalue:string 100?til 5)

myResult:select from myTable where ({x in -2#x};i) fby orderID;

q)update didEventHappen:?[(next[myType] in `TYPE_A) and raze[myvalue="0"];1b;0b] by orderID from myResult
orderID myType myvalue didEventHappen
-------------------------------------
8       TYPE_B ,"4"    0
3       TYPE_A ,"3"    0
8       TYPE_A ,"4"    0
3       TYPE_B ,"4"    0
1       TYPE_B ,"1"    0
7       TYPE_B ,"2"    0
7       TYPE_B ,"4"    0
2       TYPE_B ,"2"    0
5       TYPE_A ,"1"    0
9       TYPE_B ,"2"    0
2       TYPE_B ,"4"    0
9       TYPE_A ,"1"    0
4       TYPE_A ,"1"    0
0       TYPE_A ,"1"    0
4       TYPE_A ,"0"    0
5       TYPE_A ,"3"    0
6       TYPE_B ,"0"    1
6       TYPE_A ,"0"    0
0       TYPE_B ,"2"    0
1       TYPE_A ,"3"    0
© www.soinside.com 2019 - 2024. All rights reserved.