【问题标题】:how to pass an array into update query kdb如何将数组传递到更新查询 kdb
【发布时间】:2018-09-09 10:48:02
【问题描述】:

您好,我有以下代码,我想找到传递任意大小数组的最佳方法,而不必逐行显式执行:

这两个数组是 coeffs 和 tickers。谢谢你

t:update Value:Value*coeffs[0] from (select from t)where Code in `$tickers[0];
t:update Value:Value*coeffs[1] from (select from t)where Code in `$tickers[1];
t:update Value:Value*coeffs[2] from (select from t)where Code in `$tickers[2];
t:update Value:Value*coeffs[3] from (select from t)where Code in `$tickers[3];
t:update Value:Value*coeffs[4] from (select from t)where Code in `$tickers[4];

【问题讨论】:

    标签: kdb


    【解决方案1】:

    假设两个数组的长度相同,那么您可以尝试创建一个 tickerscoeffs 的字典:

    dict:(`$tickers)!coeffs
    

    这可以在update 语句中使用:

    update Value:Value*1^dict[Code] from t
    

    1^ 在这里至关重要,因为使用不存在的键索引到 dict 将返回 null。此表示法允许您使用1fill 空值,从而确保Value 保持不变。

    【讨论】:

      【解决方案2】:

      另一种方法是使用lj

      q)t:([] n:til 10; Value:1+til 10; Code:10#`a`b`c`d`e)
      q)tickers:enlist each "abcdf"
      

      使用tickerscoeffs 创建keyedkt

      q)kt:([Code:`$tickers] coeffs:2 4 6 8 10 )
      q)kt
      Code| coeffs
      ----| ------
      a   | 2
      b   | 4
      c   | 6
      d   | 8
      f   | 10
      

      现在加入tkt

      q)t:t lj kt
      q)t
      n Value Code coeffs
      -------------------
      0 1     a    2
      1 2     b    4
      2 3     c    6
      3 4     d    8
      4 5     e
      5 6     a    2
      6 7     b    4
      7 8     c    6
      8 9     d    8
      9 10    e
      

      更新表t,其中我们有non-null coeff

      q)update Value:Value*coeffs from t where not null coeffs
      n Value Code coeffs
      -------------------
      0 2     a    2
      1 8     b    4
      2 18    c    6
      3 32    d    8
      4 5     e
      5 12    a    2
      6 28    b    4
      7 48    c    6
      8 72    d    8
      9 10    e
      
      • 使用lj,您最终会得到一个额外的列coeffs,您可能想删除它。
      • 当您有多个映射(tickers->coeffstickers->delta 等)时,这特别有用,您只需创建一个包含所有映射的表。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-11-27
        • 2021-05-17
        • 1970-01-01
        • 2018-02-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多