【问题标题】:Select from table where column like list从表中选择列表中的列
【发布时间】:2019-06-06 09:38:11
【问题描述】:

我有下表:

t : ([]date:.z.D - til 3; min_max: ((0w;0w);(5;8);(10;15)))

看起来像这样:

date       min_max
------------------
2019.06.06 0w 0w  
2019.06.05 5  8   
2019.06.04 10 15  

请注意,min_max 列是一个列表,因此它的类型 = "F"

问题:

如何选择min_max = (0w;0w) 所在的行,这样结果查询将只给出第一行:

date       min_max
------------------
2019.06.06 0w 0w  

【问题讨论】:

    标签: kdb


    【解决方案1】:

    这样做的一种方法是使用以下选择语句,该语句将 match 与每个左副词一起使用:

    q)select from t where min_max ~\: (0w;0w)
    date       min_max
    ------------------
    2019.06.06 0w 0w
    q)\ts:1000 select from t where min_max ~\: (0w;0w)
    2 1904
    

    或者,您也可以使用以下使用in 关键字的语句。这两个查询在速度方面相当,但第二个查询使用更多内存:

    q)select from t where min_max in enlist (0w;0w)
    date       min_max
    ------------------
    2019.06.06 0w 0w
    q)\ts:1000 select from t where min_max in enlist (0w;0w)
    2 1936
    

    【讨论】:

      【解决方案2】:

      可以使用匹配(~)来实现:

      q)select from t where min_max ~\: (0w;0w)
      date       min_max
      ------------------
      2019.06.06 0w 0w
      

      https://code.kx.com/v2/ref/match/

      【讨论】:

        【解决方案3】:

        有几种方法可以做到这一点:

        使用“?” (获得第一次出现)

        q) select from t where i in min_max?enlist 2#0w
        

        使用 '='(获取所有匹配项)

        q)  select from t where all@'min_max=\:2#0w
        

        输出

        date       min_max
        ------------------
        2019.06.06 0w 0w  
        

        【讨论】:

        • 我认为? 只会返回第一次出现
        • 是的,没错。我已经提到了一种只获得第一行的解决方案和一个获得全部的解决方案。我应该在帖子中提到这一点。现在添加。
        猜你喜欢
        • 2018-02-17
        • 2018-03-28
        • 1970-01-01
        • 1970-01-01
        • 2022-09-28
        • 1970-01-01
        • 2013-10-02
        • 1970-01-01
        相关资源
        最近更新 更多