SQL Server time increment loop

Source: Internet
Author: User
Tags date1

--Query the number of tickets that a specified vendor has specified for a period of time

If every query one months, modify a time too troublesome, write a cycle!

DECLARE @date1 date

DECLARE @date2 date

DECLARE @startdate date

DECLARE @enddate date

DECLARE @countsum int

DECLARE @count int

Set @startdate = ' 2010-06-01 '

Set @enddate = ' 2011-07-01 '

Set @count =1

Set @countsum =datediff (MM, @startdate, @enddate)

Set @date1 = @startdate

Set @date2 =dateadd (mm,1, @startdate)

While @count < @countsum

Begin

--Execute SQL

SELECT SUM (Passengercount) as number from dbo. Orders WHERE ProviderID =

(SELECT UserId from dbo.) User

WHERE username= ' wind2006 ')

and Ordertime Between @date1 and @date2

Set @date1 =dateadd (mm,1, @date1)

Set @date2 =dateadd (mm,1, @date2)

Set @count = @count +1

End

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/database/SQLServer/

Related Article

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.