SQL按日累计

时间:2012-09-13 20:03:49

标签: sql oracle

我有一个简单的查询,白天吐出new_users ...

Select trunc(create_dtime),Count(Player_Id) as New_Users
  From Player
 Where Trunc(Create_Dtime) >= To_Date('2011-APR-22','yyyy-mon-dd')
 Group By Trunc(Create_Dtime)
 Order by trunc(Create_Dtime)

我希望这可以在每天继续使用new_users ...所以例如,如果使用当前查询吐出结果:

Apr 22
Apr 23
Apr 24 of 1000 new users each day...

我希望这个新查询说:

Apr 22 = 1000 New users
Apr 23 = 2000
Apr 24 = 3000

...依此类推,直到最后一个日期(今天)有一个新行的新用户总数。

请告诉我这是否足够清楚。

1 个答案:

答案 0 :(得分:1)

我会使用累积总和来做到这一点,Oracle很乐意通过分析函数来支持:

select thedate, New_Users,
       sum(New_Users) over (order by thedate) as Cumulative_New_Users
from (Select trunc(create_dtime) as thedate, Count(Player_Id) as New_Users
      From Player
      Where Trunc(Create_Dtime) >= To_Date('2011-APR-22','yyyy-mon-dd')
      Group By Trunc(Create_Dtime)
     ) t
Order by 1