Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
检查kdb中表中是否存在子表_Kdb - Fatal编程技术网

检查kdb中表中是否存在子表

检查kdb中表中是否存在子表,kdb,Kdb,我有一个表“t”和一个子表“st”。 如果子表的值与表的值匹配,则我们希望在名为“exists”的新列中使用值Y更新“t”表,否则为N Table: q)t:([] id:("ab";"cd";"ef";"gh";"ij"); refid:("";"ab";"";"ef";""); typ:`BUY`SELL`BUY`SELL`BUY) q)t id refid typ --------------- "ab" "" BUY "cd" "ab" SELL "ef" "" BUY

我有一个表“t”和一个子表“st”。
如果子表的值与表的值匹配,则我们希望在名为“exists”的新列中使用值Y更新“t”表,否则为N

Table:
q)t:([] id:("ab";"cd";"ef";"gh";"ij"); refid:("";"ab";"";"ef";""); typ:`BUY`SELL`BUY`SELL`BUY)
q)t
id   refid typ
---------------
"ab" ""    BUY
"cd" "ab"  SELL
"ef" ""    BUY
"gh" "ef"  SELL
"ij" ""    BUY

subtable:
q)st:([] id:("ab";"cd"); typ:`BUY`SELL)
q)st
id   typ
---------
"ab" BUY
"cd" SELL

Desired Output:
id   refid typ  exists
----------------------
"ab" ""    BUY  Y
"cd" "ab"  SELL Y
"ef" ""    BUY  N
"gh" "ef"  SELL N
"ij" ""    BUY  N

尝试了各种方法,如任意、每种、每种、每种、每种,但都可以得到想要的结果。

一个选项是使用
左连接(lj)


这里我定义了
id
typ
列作为
st
表的键。将表
st
的键更改为要与表
t
匹配的列一个选项是使用
左连接(lj)


这里我定义了
id
typ
列作为
st
表的键。将table
st
的键更改为要与table
t
匹配的列,Rahul的答案比下面的答案要简洁得多,但下面的选项可能更有效,具体取决于表的大小。通过将
t
中的列匹配到
st
(索引和翻转)并确定匹配行的布尔值,将其传递到YN布尔字典中,我们可以生成所需的结果

q)`N`Y (flip cols[st]!t[cols[st]]) in st
`Y`Y`N`N`N
q)update exists:(`N`Y (flip cols[st]!t[cols[st]]) in st) from t
id   typ  refid exists
----------------------
"ab" BUY  ""    Y
"cd" SELL "ab"  Y
"ef" BUY  ""    N
"gh" SELL "ef"  N
"ij" BUY  ""    N
q)\t:100000 update exists:(`N`Y (flip cols[st]!t[cols[st]]) in st) from t
446
q)\t:100000 update `N^exists from t lj `id`typ xkey update exists:`Y from st
856
一旦应用了属性,Rahuls lj方法可能会推广得更快


根据Rahuls的观察编辑,

Rahul的答案比下面的答案要简洁得多,但下面的答案可能更有效,具体取决于您的表格大小。通过将
t
中的列匹配到
st
(索引和翻转)并确定匹配行的布尔值,将其传递到YN布尔字典中,我们可以生成所需的结果

q)`N`Y (flip cols[st]!t[cols[st]]) in st
`Y`Y`N`N`N
q)update exists:(`N`Y (flip cols[st]!t[cols[st]]) in st) from t
id   typ  refid exists
----------------------
"ab" BUY  ""    Y
"cd" SELL "ab"  Y
"ef" BUY  ""    N
"gh" SELL "ef"  N
"ij" BUY  ""    N
q)\t:100000 update exists:(`N`Y (flip cols[st]!t[cols[st]]) in st) from t
446
q)\t:100000 update `N^exists from t lj `id`typ xkey update exists:`Y from st
856
一旦应用了属性,Rahuls lj方法可能会推广得更快


根据Rahuls观察结果进行编辑,

其他答案的另一种替代方法是从
t
cols[st]#t
)中提取子表列,并检查它们是否位于
st

update exists:(cols[st]#t)in st from t
id   refid typ  exists
----------------------
"ab" ""    BUY  1
"cd" "ab"  SELL 1
"ef" ""    BUY  0
"gh" "ef"  SELL 0
"ij" ""    BUY  0
如果需要将结果显示为YN,则可以进行轻微修改以获得以下结果:

update exists:`N`Y(cols[st]#t)in st from t
id   refid typ  exists
----------------------
"ab" ""    BUY  Y
"cd" "ab"  SELL Y
"ef" ""    BUY  N
"gh" "ef"  SELL N
"ij" ""    BUY  N

其他答案的另一种选择是从
t
cols[st]#t
)中提取子表列,并检查它们是否位于
st

update exists:(cols[st]#t)in st from t
id   refid typ  exists
----------------------
"ab" ""    BUY  1
"cd" "ab"  SELL 1
"ef" ""    BUY  0
"gh" "ef"  SELL 0
"ij" ""    BUY  0
如果需要将结果显示为YN,则可以进行轻微修改以获得以下结果:

update exists:`N`Y(cols[st]#t)in st from t
id   refid typ  exists
----------------------
"ab" ""    BUY  Y
"cd" "ab"  SELL Y
"ef" ""    BUY  N
"gh" "ef"  SELL N
"ij" ""    BUY  N

您需要将
c
更改为cols[st],否则它将给出一个错误,因为qsql的这一部分不允许赋值。此外,命令可以缩短为-`update exists:`N`Y(cols[st]#t)in st from t`您需要将
c
更改为cols[st],否则它将给出错误,因为qsql的此部分不允许赋值。此外,命令可以缩短为-`updateexists:`N`Y(cols[st]#t)in st from t`