我要循环一段时间查询统计 然后累加起来等到总数。
如:20090801-20090803select sum(*) from a where sj='20090801'
select sum(*) from a where sj='20090802'
select sum(*) from a where sj='20090803'我想用个变量来替换时间最后再求个总和(也就是这3天,每天等到的数量的总和)。
如:20090801-20090803select sum(*) from a where sj='20090801'
select sum(*) from a where sj='20090802'
select sum(*) from a where sj='20090803'我想用个变量来替换时间最后再求个总和(也就是这3天,每天等到的数量的总和)。
dtime date;
sdate varchar2(20); ibegin number;
iend number;
begin
dtime := to_date('2009-08-01', 'yyyy-mm-dd');
ibegin := 0;
iend := 2; for i in ibegin..iend
loop
sdate := to_char(dtime + i, 'yyyy-mm-dd');
dbms_output.put_line(sdate);
end loop;
end;这个可以将日期循环起来
可以用connect by的语法比如select sysdate+rownum from dual connect by rownum<=7; 不过9i里有bug只有1条返回,10g已经修改过,可以返回7条
LOOP里面可以直接写SQL,只要在声明一个数组,将查询结果INTO就OK了。
count(*) over(partition by sj) 每天登录量,
from a where sj between '20090801' and '20090803'
如果在存储过程中
可以创建一个带参数的游标
可以创建一个动态sql文
或者用for idx in 1..3
end的形式也可以
i number;
tmpval number;
tmptotal number;
maxday number;
begin
i:=20090801;
maxday := 20090803;
tmptotal := 0;
while i<=maxday loop
select count(*) into tmpval from a where sj=to_char(i);
tmptotal := tmptotal + tmpval;
i := to_char(to_date(i,'yyyymmdd')+1,'yyyymmdd');
end loop;
dbms_output.put_line('总合计是:'+tmptotal);
end;