Search code examples
kdb

kdb/q -- how do I select by customized time list?


I know I can select by sym, time.second from table to select by seconds or use xbar for some other fixed time intervals

But they are more or less uniformed time intervals. How do I supplied a completely customized list of times into the select by?


Solution

  • you could use binr to accomplish this

    q)tab:([]t:.z.d+10000?.z.n)
    q)/ create custom bins ensuring in ascending time order for binr to work correctly 
    q)tl:.z.d+09:30 10:45 15:32
    q)/ x binr y - will return the index of first item in x which is ≥y 
    q)/ we then index back into our tl with result 
    q)select count i by tl tl binr t from tab 
    t                            | x   
    -----------------------------| --
    2019.03.31D09:30:00.000000000| 6443
    2019.03.31D10:45:00.000000000| 807 
    2019.03.31D15:32:00.000000000| 2750