非常简单,愚蠢的问题。请考虑下表:
tt:([]Id:`6`7`12 ;sym:`A`B`C;symlist:((`A`B`M);(`X`Y`Z);(`H`F`C)))
Id sym symlist
---------------
6 A `A`B`M
7 B `X`Y`Z
12 C `H`F`C
我想选择tt中的所有行,其中sym中的元素包含在列表symlist中。在这种情况下,它仅表示第一行和第三行。但是,以下查询给出了类型错误。
select from tt where sym in symlist
(`type)
这样做的正确方法是什么?感谢
答案 0 :(得分:3)
你想使用'
(每个两个)副词,以便他们“配对”可以这么说。回想一下,sym
只是列表,symlist
是列表。您想要使用sym
中的相应子列表检查symlist
中的每个元素。你通过告诉它“配对”来做到这一点。
q)tt:([]id:6712; sym:`A`B`C; symlist:(`A`B`M;`X`Y`Z;`H`F`C))
q)select from tt where sym in'symlist
id sym symlist
----------------
6712 A A B M
6712 C H F C
我不完全清楚为什么您的查询会导致类型错误,因此我有兴趣听取其他人的回复。
q)select from tt where sym in symlist
'type
in
`A`B`C
(`A`B`M;`X`Y`Z;`H`F`C)
q)select from tt where {x in y}[sym;symlist]
id sym symlist
--------------
答案 1 :(得分:1)
回应JPC的回答(不能将此格式化为评论)....
类型错误可能是由于应用"其中"标量布尔值
q)(`a`b`c) in (`a`g`b;`u`i`o;`g`c`t)
0b
q)where (`a`b`c) in (`a`g`b;`u`i`o;`g`c`t)
'type
另外,{x in y} lambda导致错误的原因是" in"被遮盖,并且解析器看不到(解析器看不到lambda内部)
q)0N!parse"select from tt where {x in y}[sym;symlist]";
(?;`tt;,,({x in y};`sym;`symlist);0b;())
而解析器可以"看" " in"在第一种情况下
q)0N!parse"select from tt where sym in symlist";
(?;`tt;,,(in;`sym;`symlist);0b;())
我猜测解析器在看到" in"
时会尝试做一些优化