Write a convenient SQL loop to check the data for each day

Source: Internet
Author: User

Declare @recd int,@i int,@a int,@count int,@day1Date@day2Date@days intSet @recd=0Set @i=0Set @a=0Set @day1='2014-8-24'Set @day2='2014-8-25'Set @days= A --Set 12 daysCreate Table#tpp (IDint IDENTITY(1,1) not NULL,--Create a column ID, and add 1 each time you add a recorddateshow Date, dateend date, numint, Paynumint, Paymoneyint, Payordernumint    Primary Key(ID)--defines the primary key for the #tmp of the temporary table ID); while(@i<@days)begin    if @i>0     begin    Set @a=1    End    Set @day1=DATEADD( Day,@a,@day1)    Set @day2=DATEADD( Day,@a,@day2)        SELECTUseridCOUNT(1) asDanshu,sum(Paymoney) asSump into#pp from [Table] wherePaytimebetween @day1  and @day2  andStatus>0 Group  byUserID
Insert#tppSelect @day1 asDay1,@day2 asDay2,@i,(Select COUNT(1) from#pp) asNumber of recharge, (Select SUM(sump) from#pp) asRecharge Amount, (Select SUM(Danshu) from#pp) asTop-up singularDrop Table#ppSet @i=@i+1EndSelect * from#tppDrop Table#tpp

The daily query demand is too frequent, think of a way to give everything to the computer to complete, long time not come. Come up today and turn around.

Write a convenient SQL loop to check the data for each day

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.