【问题标题】:KDB rolling sumKDB 滚动总和
【发布时间】:2017-09-15 06:07:53
【问题描述】:

我有一张桌子

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

我想获得同一天每个 sym 的累计数量

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

但我只设法将具有相同键 datets`sym 的行折叠到一个列表中,但它并没有给我一个滚动总和。有什么建议吗?

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

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

【问题讨论】:

    标签: kdb


    【解决方案1】:

    这应该做你想做的:

    //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
    

    【讨论】:

      【解决方案2】:

      这可能有用,虽然有点难看;

      `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 中聚合数据。可能有一种方法可以隐式执行此操作,但我现在不会。

      【讨论】:

        【解决方案3】:

        我可能误解了你的问题..所以你想要匹配 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
        

        编辑:我相信您可以通过功能更新使其更漂亮 (http://www.timestored.com/kdb-guides/functional-queries-dynamic-sql) 我只是自己编写了一些函数来向您展示基本思想。 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]);
                 };
        
        1. 通过每个权限 (/) 更新表格

           temp2:{[tbl; idx]
              row: first select from tbl where i = idx;
              :tbl lj (`date`sym`ts xkey enlist last update cumulative:sums qty from  (select from tbl where date=row[`date],sym=row[`sym],ts<=row[`ts]));
              };
          

        对于 #1,您可以调用如下内容:

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

        对于 #2,您可以调用如下代码:

        tbl: temp2/[tbl; til count tbl]
        

        【讨论】:

          【解决方案4】:

          如果行是按时间顺序排列的,则无需对表进行排序:by 子句将满足您的需求。

          1. 使用update 通过datets 计算cumsum
          2. 通过datetssym选择cumsumlast
          3. 删除密钥
          
          q)0!select last cumsum by date,ts,sym from update cumsum: sums qty by date,sym from t
          date ts sym cumsum
          ------------------
          d1   t1 s1  -200
          d1   t2 s2  200
          d1   t3 s1  0
          d1   t4 s1  500
          d1   t5 s2  -100
          d2   t1 s1  -400
          

          如果您需要参数化其中任何一项(即,将列名作为参数传递),您将需要functional forms:

          q)u:![t;();`date`sym!`date`sym;(enlist`cumsum)!enlist(sums;`qty)]
          q)0!?[u;();`date`ts`sym!`date`ts`sym;(enlist`cumsum)!enlist(last;`cumsum)]
          

          更多Q for Mortals: §9. Queries

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2013-08-13
            • 2015-07-21
            • 2019-04-11
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-11-05
            • 1970-01-01
            相关资源
            最近更新 更多