码迷,mamicode.com
首页 > 数据库 > 详细

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

时间:2014-09-05 16:02:51      阅读:319      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   ar   数据   2014   div   sp   log   

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 循环查每一天的数据

标签:style   blog   color   ar   数据   2014   div   sp   log   

原文地址:http://www.cnblogs.com/fangyuan303687320/p/3958141.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!