Kdb 滚动和

Kdb 滚动和,kdb,Kdb,我有一张桌子 t:flip `date`sym`ts`qty!(`d1`d1`d1`d1`d1`d1`d2;`s1`s1`s2`s1`s1`s2`s1;`t1`t1`t2`t3`t4`t5`t1;-100 -100 200 200 500 -300 -400) date sym ts qty d1 s1 t1 -100 d1 s1 t1 -100 d1 s2 t2 200 d1 s1 t3

我有一张桌子

t:flip `date`sym`ts`qty!(`d1`d1`d1`d1`d1`d1`d2;`s1`s1`s2`s1`s1`s2`s1;`t1`t1`t2`t3`t4`t5`t1;-100 -100 200 200 500 -300 -400)

date    sym   ts     qty
d1       s1   t1    -100
d1       s1   t1    -100
d1       s2   t2     200
d1       s1   t3     200
d1       s1   t4     500
d1       s2   t5    -300
d2       s1   t1    -400
我想得到当天每个符号的累计数量总和

date    sym   ts     qty   cumsum
d1       s1   t1    -100     -200 // -100 - 100
d1       s2   t2     200      200 //  200
d1       s1   t3     200        0 // -100 -100 + 200
d1       s1   t4     500      500 // -100 -100 + 200 + 500
d1       s2   t5    -300     -100 //  200 - 300
d2       s1   t1    -400     -400 // -400 (date is d2)
我试着用

select sums qty by date, ts, sym from t
但是有了这个,我只能将具有相同键dates`sym的行折叠到一个列表中,但它不能给我一个滚动总和。有什么建议吗

编辑: 所以,基本上我想附加一列,显示我从这个查询中得到的值

select sum qty from t where sym =`symbol_of_this_row, ts <= ts_of_this_row, date = _date_of_this_row

我可能误解了你的问题。。所以你想得到与datesym`timestamp匹配的行的累计和,是吗

这个怎么样:

    t: update cumsum:sums qty by date, sym, ts from t
    // for the sake of 'pretty view' sort by `date`sym`ts 
    `date`sym`ts xasc t
编辑:我相信你可以通过功能更新使它更漂亮 我只是自己写了一些函数来告诉你基本的想法。 1.传递表格和表格的每一行

    temp:{[idx; tbl]
         row: first select from tbl where i = idx;
         : last update cumulative:sums qty from (select from tbl where date=row[`date], sym=row[`sym], ts<=row[`ts]);
         };
对于2,您可以调用如下内容:

tbl: {: temp[y; x] }[; tbl] each til count tbl
tbl: temp2/[tbl; til count tbl]

我可能误解了你的问题。。所以你想得到与datesym`timestamp匹配的行的累计和,是吗

这个怎么样:

    t: update cumsum:sums qty by date, sym, ts from t
    // for the sake of 'pretty view' sort by `date`sym`ts 
    `date`sym`ts xasc t
编辑:我相信你可以通过功能更新使它更漂亮 我只是自己写了一些函数来告诉你基本的想法。 1.传递表格和表格的每一行

    temp:{[idx; tbl]
         row: first select from tbl where i = idx;
         : last update cumulative:sums qty from (select from tbl where date=row[`date], sym=row[`sym], ts<=row[`ts]);
         };
对于2,您可以调用如下内容:

tbl: {: temp[y; x] }[; tbl] each til count tbl
tbl: temp2/[tbl; til count tbl]

这应该满足您的要求:

//Ascend by date and time to make sure that result sets match
`date`ts xasc 
    //Compute cumulative sums by date, sym, timestamp
    update sums cumul by date,sym from 
        //Make sure that there is a single qty for each timestamp
        select cumul:sum qty by date,sym,ts from t

这应该满足您的要求:

//Ascend by date and time to make sure that result sets match
`date`ts xasc 
    //Compute cumulative sums by date, sym, timestamp
    update sums cumul by date,sym from 
        //Make sure that there is a single qty for each timestamp
        select cumul:sum qty by date,sym,ts from t

这也许管用,虽然有点难看

`date`ts xasc 0! / sort and unkey
    update cumsum:sums qty by date, sym from 
        select sum qty by date, sym, ts from t
它产生

date sym ts qty  cumsum
-----------------------
d1   s1  t1 -200 -200  
d1   s2  t2 200  200   
d1   s1  t3 200  0     
d1   s1  t4 500  500   
d1   s2  t5 -300 -100  
d2   s1  t1 -400 -400  

请注意,第一行中的数量与您的示例不同。这是因为在运行累积和之前,我必须在相同的ts内聚合数据。也许有一种方法可以做到这一点,但我现在不会想到

`date`ts xasc 0! / sort and unkey
    update cumsum:sums qty by date, sym from 
        select sum qty by date, sym, ts from t
它产生

date sym ts qty  cumsum
-----------------------
d1   s1  t1 -200 -200  
d1   s2  t2 200  200   
d1   s1  t3 200  0     
d1   s1  t4 500  500   
d1   s2  t5 -300 -100  
d2   s1  t1 -400 -400  

请注意,第一行中的数量与您的示例不同。这是因为在运行累积和之前,我必须在相同的ts内聚合数据。可能有一种方法可以隐式地执行此操作,但我现在不会想到。

如果行是按时间顺序排列的,则无需对表进行排序:by子句将执行您想要的操作

使用update按日期和ts计算累计和 按日期、ts和sym选择cumsum的最后一个值 删除密钥 如果需要对其中任何一项进行参数化,即将列名作为参数传递,则需要:


更多信息参见

如果行是按时间顺序排列的,则无需对表进行排序:by子句将执行您想要的操作

使用update按日期和ts计算累计和 按日期、ts和sym选择cumsum的最后一个值 删除密钥 如果需要对其中任何一项进行参数化,即将列名作为参数传递,则需要:

更多