写一段方便的SQL 循环查每一天的数据

declare @recd int,@i int,@a int,@count int,@day1 date,@day2 date,@days int
set @recd=0
set @i=0
set @a=0
set @day1=2014-8-24
set @day2=2014-8-25
set @days=12 --设定12天
create table #tpp
(
    ID   int IDENTITY (1,1)     not null, --创建列ID,并且每次新增一条记录就会加1
    dateshow     date,
    dateend   date,
    num     int,   
    paynum  int,
    paymoney int,
    payordernum int
    primary key (ID)      --定义ID为临时表#Tmp的主键      
);
while(@i<@days)
begin

    if @i>0 
    begin
    set @a=1
    end
    set @day1=DATEADD(DAY,@a,@day1)
    set @day2=DATEADD(DAY,@a,@day2)
    
    SELECT userID,COUNT(1) as danshu,sum(payMoney) as sump
into #pp FROM [表] where paytime between @day1 and @day2 and status>0 group by userID
insert #tpp select @day1 as day1,@day2 as day2,@i,(select COUNT(1) from #pp )as 充值人数,(select SUM(sump) from #pp) as 充值金额,(select SUM(danshu) from #pp)as 充值单数 drop table #pp set @i=@i+1 end select * from #tpp drop table #tpp

每天的查询需求太频繁,想了一个办法把所有事情交给计算机来完成,好久不来。今天上来转转

写一段方便的SQL 循环查每一天的数据

上一篇:oracle11g rac listener offline


下一篇:MySQL锁阻塞分析