写一段方便的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
每天的查询需求太频繁,想了一个办法把所有事情交给计算机来完成,好久不来。今天上来转转
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。