寫一段方便的SQL 迴圈查每一天的資料

來源:互聯網
上載者:User

標籤:style   blog   color   ar   資料   2014   div   sp   log   

declare @recd int,@i int,@a int,@count int,@day1 date,@day2 date,@days intset @recd=0set @i=0set @a=0set @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 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 迴圈查每一天的資料

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.