【问题标题】:How to check if some values have been above a threshold for more than n days?如何检查某些值是否超过阈值超过 n 天?
【发布时间】:2020-03-23 19:46:48
【问题描述】:

我有下表:

t:([]date:.z.d+neg til 100; region:100#(`us`eu`asia`au); product:100#(`car`insurance`loan`other); qty:100?1f);

date       region product   qty        
---------------------------------------
2019.11.28 us     car       0.3998898  
2019.11.27 eu     insurance 0.592308   
2019.11.26 asia   loan      0.9796166  
...

我想要检查qty 是否超过给定阈值,比如0.5,超过n 天,比如3 天。这里days实际上可以是连续观察的数量。

我想按地区检查我拥有的 4 种产品中的每一种。

我考虑过在n 天应用滚动函数,并应用以下函数:

myfunc:{[l]
    :all l>0.5;
    };

0.5 是我的阈值,但它并没有真正起作用......

【问题讨论】:

    标签: group-by kdb rolling-computation


    【解决方案1】:

    使用 fby 的一种方法:

    q)consec:{s in(s:sums[differ y])where(x-1){x&prev x}/y};
    q)`region`product`date xdesc select from t where (consec[3];0.5<qty)fby([]region;product)
    date       region product   qty
    -------------------------------------
    2019.11.04 us     car       0.949975
    2019.10.31 us     car       0.8481567
    2019.10.27 us     car       0.9367503
    2019.09.28 eu     insurance 0.6884756
    2019.09.24 eu     insurance 0.9598964
    2019.09.20 eu     insurance 0.6789082
    2019.09.16 eu     insurance 0.726781
    2019.09.12 eu     insurance 0.5830262
    2019.09.08 eu     insurance 0.7750292
    2019.11.21 au     other     0.5347096
    2019.11.17 au     other     0.5785203
    2019.11.13 au     other     0.6137452
    2019.11.09 au     other     0.6919531
    2019.09.30 au     other     0.7278528
    2019.09.26 au     other     0.7520102
    2019.09.22 au     other     0.6430982
    2019.09.18 au     other     0.9877844
    2019.09.14 au     other     0.8355065
    2019.09.10 au     other     0.9149882
    2019.09.06 au     other     0.6324114
    
    /to make it a sequence of 5 consecutive
    `region`product`date xdesc select from t where (consec[5];0.5<qty)fby([]region;product)
    
    /to make the threshold >0.6
    `region`product`date xdesc select from t where (consec[5];0.6<qty)fby([]region;product)
    

    【讨论】:

      猜你喜欢
      • 2013-05-05
      • 2020-12-24
      • 2022-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-24
      • 1970-01-01
      相关资源
      最近更新 更多