Simple SQL Time series generation, 10 minutes each time interval.

Source: Internet
Author: User
Tags time interval

CREATE Table #timeseries (times datetime NOT NULL)
Go
DECLARE @firstdate datetime, @lastdate datetime
DECLARE @MI int
Begin
Set @firstdate = ' 8/8/2012 6:00 '
Set @lastdate = ' 8/9/2012 6:00 '
Set @MI =datediff (MI, @firstdate, @lastdate)
--Nested Loops
while (@MI >=0)
Begin
INSERT INTO #timeseries
VALUES (@firstdate)
SET NOCOUNT on
Set @firstdate =dateadd (mi,10, @firstdate)-increments by 10 minutes per start time.
Set @MI =datediff (MI, @firstdate, @lastdate)-when @firstdate is incremented to @lastdate phase, MI is 0, exit loop
SET NOCOUNT Off
End
End
Select Row_number () over (order by times) as ID, CONVERT (varchar), times, as date, times from #timeseries order by Ti MES ASC
drop table #timeseries

Simple SQL Time series generation, 10 minutes each time interval.

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.