0

假设不知道过滤器列是否存在的情况。

t:([]a:`s1`s2`s3; c:1 2 3);
select c from t where null t[`a]
select c from t where null t[`b]
'length
(where null t[`a])~where null t[`b]
1b

它列a存在,然后就可以了select

但是当我使用带有列的过滤器b(不存在)时,我得到一个错误。

  1. 为什么会这样?- 我检查了两个where结果 - 它们是相同的
  2. 如何解决这种情况?
4

2 回答 2

2

在 QSQL 中,“where”需要作用于与表长度相同或计数为 1 的布尔值列表。示例的计数为零

q)t[`b]
`symbol$()
q)count t`b
0

一些例子:

q)select from t where 0b
a c
---
q)select from t where 00b
'length
  [0]  select from t where 00b
       ^
q)select from t where 000b
a c
---
q)select from t where 0000b
'length
  [0]  select from t where 0000b
       ^
q)select from t where 0#0b
'length
  [0]  select from t where 0#0b

更新:因为你提到了“where”的结果,where 被包装在 QSQL 格式中,而不是独立行动然后应用结果,所以你看到下面的区别

q)where 00b
`long$()
q)select from t where 00b
'length
  [0]  select from t where 00b
       ^
q)select from t `long$()
a c
---
于 2020-10-15T11:31:30.527 回答
1

您可以使用@运算符来捕获错误,例如

q)@[{select c from t where null t[x]};`b;{x}]
"length"
q)@[{select c from t where null t[x]};`a;{x}]
c
-

虽然您不需要像这样引用列,但如果您想动态选择列,您可以使用功能选择,因此功能形式的等效查询将是

q)?[`t;enlist(null;`a);0b;(enlist `c)!(enlist `c)]
c
-

这里的语法非常棘手,但parse关键字对于找出您应该使用的参数非常有用,例如

q)parse"select c from t where null a"
?
`t
,,(^:;`a)
0b
(,`c)!,`c

向您展示您需要的论据。错误捕获和功能查询的组合应该可以帮助您解决您在此处尝试实现的目标。

于 2020-10-15T12:05:24.990 回答