【问题标题】:Kdb find first not null valuekdb首先找到非空值
【发布时间】:2014-02-24 12:15:58
【问题描述】:

在 KDB 中进行分组时。我必须在该组中为一列找到第一个非空值

例如

t:([]a:1 1 1 2;b:0n 1 3 4 )
select first b by a from t

我找到了一种方法来实现这一点:

select first b except 0n by a from t

我不确定这是否是正确的方法。请提供建议。

【问题讨论】:

    标签: group-by kdb notnull


    【解决方案1】:

    这对我来说似乎是个好方法。

    两种选择包括:

    select first b where not null b by a from t
    

    好处是它不依赖于特定的列类型,也许更清楚地解释了您的意图,但它会稍长一些。或者

    select b:last fills reverse b by a from t
    

    在某些测试运行中哪个是最快的方法。

    在 kdb 中,做事总是有多种方法,但从来没有真正正确或错误的答案。

    【讨论】:

    • 感谢约翰的回复。我不知道上述方法。我运行了以下查询以查看哪个工作得更快
      t :([]a:1000000?100;b:1000000?100)
      meta t
      选择第一个 b 除了 0Nj by a from t
      select first b where not null b by a from t
      select b:last 用 a from t 填充反向 b
      看起来第一个和第二个查询比第三个查询快。在某些情况下,第 3 种可能会是更好的选择
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-15
    • 2014-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多