【问题标题】:KDB+/Q: How to get all rows within a given time or within a given number of rows?KDB+/Q:如何获取给定时间内或给定行数内的所有行?
【发布时间】:2020-06-25 08:18:47
【问题描述】:

如何使用 KDB+/Q 在时间或行计数中创建具有回溯窗口的选择?

即给定下表:

time (seconds) | val
---------------------
1              | 5
2              | 6
3              | 7
4              | 8
6              | 9

如何最有效:

  • a) 选择获取最后一行和给定最后一行的前 2 行,即([]time:3,4,6; val:7,8,9)
  • b) 选择大于或等于 2 秒的事件,即([]time:4,6; val:8,9)

我已经尝试过 wj 和 xbar,它们都可以在整个表格上运行良好,但是,我正在寻找可以应用于单行的本实例中的 2 个函数(及其各自的回溯窗口)有效。

谢谢

【问题讨论】:

    标签: kdb


    【解决方案1】:

    这是一种解决您的两个问题的通用方法:

    q)t:([]time:1 2 3 4 6;val:5 6 7 8 9)
    
    /get last row and previous 2
    q)select from t where {x|next x}/[2;time=6]
    time val
    --------
    3    7
    4    8
    6    9
    
    /or alternatively (depending on exactly how you define "last row")
    select from t where {x|next x}/[2;time=max time]
    select from t where {x|next x}/[2;i=4]
    
    /events >=2 seconds old (and the previous 1)
    q)select from t where {x|next x}/[1;2<=deltas time]
    time val
    --------
    4    8
    6    9
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-02
      • 1970-01-01
      • 1970-01-01
      • 2012-11-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多