kdb

a list of N subsequent elements in KDB


Given the table t: ([] a: 1 2 3 4), how can one obtain the column b for e.g.,

n=2

a b
-----
1 1 2
2 2 3
3 3 4
4 ,4

n=3

a b
-------
1 1 2 3
2 2 3 4
3 3 4
4 ,4

where n is the number of subsequent elements of a (including a) to be included in b.


Solution

  • Use next and scan. The left argument is the number of subsequent elements you want:

    q)update b:flip 1 next\a from t
    a b
    ----
    1 1 2
    2 2 3
    3 3 4
    4 4
    q)update b:flip 2 next\a from t
    a b
    ------
    1 1 2 3
    2 2 3 4
    3 3 4
    4 4