首页 > 代码库 > 写一段方便的SQL 循环查每一天的数据

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

declare @recd int,@i int,@a int,@count int,@day1 date,@day2 date,@days intset @recd=0set @i=0set @a=0set @day1=2014-8-24set @day2=2014-8-25set @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 sumpinto #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+1endselect * from #tppdrop table #tpp

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

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