【问题标题】:Select non-empty string rows in KDB在 KDB 中选择非空字符串行
【发布时间】:2018-09-25 01:24:05
【问题描述】:
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     ""    

我只想选择“详细信息”非空的行。看起来相当简单,但不是我无法让它工作。

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

这仍然是所有行。

我也试过了

q) select from tab where not null detail 

这给了我类型错误。

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

【问题讨论】:

    标签: kdb


    【解决方案1】:

    您可以使用like 来简化这一点,而不是使用副词。

    q)select from tab where not detail like ""
    
      items sales prices detail
      ------------------------------
      nut   1     10     "blah"
      cam   5     9      "some text"
    

    【讨论】:

      【解决方案2】:

      由于您需要逐行执行检查,请使用each

      select from tab where 0 < count each detail
      

      这会产生下表:

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

      【讨论】:

        【解决方案3】:

        使用副词 each:

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

        【讨论】:

          【解决方案4】:

          我会使用以下方法

          select from tab where not detail~\:""
          

          每个细节都与空字符串进行比较。使用not null detail 的方法不起作用,因为 Q 将字符串视为字符数组并检查每个数组元素是否为空。 IE。 null "abc" 返回布尔数组 000b,但 where 子句要求每个“行”的单个布尔值

          【讨论】:

          • 感谢您的详细解释。现在我更好地理解了底层结构。
          【解决方案5】:

          如果您的表不大,您可以检查的另一种方法是在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"
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2023-03-30
            • 1970-01-01
            • 2013-04-29
            • 2017-06-12
            • 1970-01-01
            • 2013-01-28
            • 1970-01-01
            相关资源
            最近更新 更多