Select KDB 中的非空字符串行

Select non-empty string rows in KDB

q)tab

items sales prices detail
-------------------------
nut   6     10     "blah"    
bolt  8     20     ""
cam   0     15     "some text"    
cog   3     20     ""    
nut   6     10     ""    
bolt  8     20     ""    

我想 select 仅 "detail" 非空的行。看起来相当简单,但我无法让它工作。

q) select from tab where count[detail] > 0

这会给出所有行。

或者我试过

q) select from tab where not null detail 

这给我打字错误。

如何在 KDB 中查询非空字符串字段???

因为您需要逐行执行检查,所以使用 each:

select from tab where 0 < count each detail

这会产生以下结果 table:

items sales prices detail     
------------------------------
nut   6     10     "blah"     
cam   0     15     "some text"

使用副词 each:

q)select from ([]detail:("blah";"";"some text")) where 0<count each detail
detail     
-----------
"blah"     
"some text"

我会使用以下方法

select from tab where not detail~\:""

其中每个细节都与空字符串进行比较。使用 not null detail 的方法不起作用,因为 Q 将字符串视为字符数组并检查每个数组元素是否为空。 IE。 null "abc" returns 布尔数组 000b,但是 where 子句期望每个 "row"

的单个布尔值

不使用副词,您可以使用 like 来简化它。

q)select from tab where not detail like ""

  items sales prices detail
  ------------------------------
  nut   1     10     "blah"
  cam   5     9      "some text"

如果您的 table 不大,另一种检查方法是将其转换为 where 子句中的 symbol

q)select from ([]detail:("blah";"";"some text")) where `<>`$detail 
detail
-----------
"blah"
"some text"

或者干脆

q)select from ([]detail:("blah";"";"some text")) where not null `$detail 
detail
-----------
"blah"
"some text"