这条sql语句怎么写 是高手的进来看看

无情波仔 2011-12-30 02:25:16
我有一个表 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
...全文
145 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
raymonshi 2012-01-01
  • 打赏
  • 举报
回复
使用窗口函数 sum over
zlb_chen 2011-12-31
  • 打赏
  • 举报
回复

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
xpingping 2011-12-30
  • 打赏
  • 举报
回复

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

3,499

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 高级技术相关讨论专区
社区管理员
  • 高级技术社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧