【问题标题】:How to apply max function for each row in KDB?如何为 KDB 中的每一行应用 max 函数?
【发布时间】:2014-04-14 09:25:53
【问题描述】:

我想确保x 列中的所有值不小于 0.5,所以我这样做:

update x:max (x 0.5) from myTable

但这会产生错误(Studio For KDB+):

An error occurred during execution of the query.
The server sent the response:
type
Studio Hint: Possibly this error refers to wrong type, e.g `a+1

怎么了?

【问题讨论】:

    标签: kdb q-lang


    【解决方案1】:

    你可以试试|

    q)update x|0.5 from myTable
    

    【讨论】:

      【解决方案2】:

      它应该工作。它对我有用。这是我用于测试的查询:

      update x:max(x;0.5) from myTable
      

      -- 检查max函数中的分号

      【讨论】:

      • 非常好 - 我没有想到这会起作用,但确实如此。与x|.5 一样快(诚然更惯用)
      【解决方案3】:

      尝试kdb vector conditional,它类似于 SQL 中的 case-when:

      q)t:([] a:6?.9)
      
      q)t
      a
      ---------
      0.4237094
      0.5712045
      0.8705158
      0.2075746
      0.8549775
      0.3951729
      
      q)update ?[a<0.5;0.5;a] from t
      a
      ---------
      0.5
      0.5712045
      0.8705158
      0.5
      0.8549775
      0.5
      q)
      

      【讨论】:

      • 对于这种简单的情况,向量条件是不必要的,可读性较差,并且在我的机器上慢了 4 倍。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-01
      相关资源
      最近更新 更多