我有一个表 reports_user 
  字段如下:
id time     activer  register lastLoginer(time这一天登陆的用户) (其他的字段不要考虑)
218 20111109 23 56 100
217 20111203 0 0 0
216 20111204 0 0 0
215 20111205 0 0 0
214 20111206 0 0 0
213 20111207 0 0 0
212 20111208 0 0 3
211 20111209 0 0 0
210 20111210 0 0 0
209 20111211 0 0 0
208 20111212 0 0 0
207 20111213 1 1 1
206 20111214 1 1 1
205 20111215 0 0 0
204 20111216 23 45 100
203 20111217 0 0 0
202 20111218 0 0 0
201 20111219 23 100 132
200 20111220 34 123 450
199 20111221 0 0 0
198 20111222 0 0 0
197 20111223 0 0 0
196 20111224 12 45 123
195 20111225 34 100 254
194 20111226 23 45 100
我的要求是输出如下的列:
allLastLoginer:   (在time之前所有登陆过得用户) (包括time这一天)
time:            时间
lastLoginer:      在time这一天登陆过得用户
表如下
time       lastLoginer    allLastLoginer  (包括time这一天所有登陆过得用户)
20111223     23                 56

解决方案 »

  1.   


    对allLastLoginer (在time之前所有登陆过得用户) (包括time这一天)连续求和 
    sum(lastLoginer) over (order by time) 
      

  2.   


    create table taba
    (
      id number,
      time varchar2(32),
      lastloginer number
    )insert into taba values(1,'20111112',25);
    insert into taba values(2,'20111113',5);
    insert into taba values(3,'20111112',15);
    insert into taba values(4,'20111113',35);
    insert into taba values(5,'20111114',45);select time, sum(lastloginer), sum(Alllastloginer)
      from (select a.time,
                   sum(a.lastloginer) as lastloginer,
                   0 as Alllastloginer
              from taba a
             group by a.time
            union
            select a.time,
                   0 as lastloginer,
                   sum(case
                         when a.time >= b.time then
                          b.lastloginer
                         else
                          0
                       end) as Alllastloginer
              from (select a.time,
                           sum(a.lastloginer) as lastloginer,
                           0 as Alllastloginer
                      from taba a
                     group by a.time) a,
                   (select a.time,
                           sum(a.lastloginer) as lastloginer,
                           0 as Alllastloginer
                      from taba a
                     group by a.time) b
            
             group by a.time) a
     group by time