如何找到间隔100天的金额?

时间:2018-09-25 12:16:07

标签: oracle sum window-functions

如何找到间隔100天的金额? 我有这个选择:

data_final.to_excel(writer,'Sheet1',float_format='%.8f',index=False)

1 个答案:

答案 0 :(得分:0)

您正确地做到了。另一种方法是相关子查询,如下所示。但是窗口函数正是为此目的而设计的,您应该使用它们而不是子查询。它们更快。

SQLFiddle demo

select id, date_, amn, 
       sum(amn) over(partition by id order by date_ 
                     range between interval '100' day(3) preceding
                       and current row) s1, 
       (select sum(amn) 
          from ddd d2 
          where ddd.id = d2.id 
            and date_ between ddd.date_ - interval '100' day(3) 
                          and ddd.date_) s2
  from ddd