-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGenerate 5 min table.sql
80 lines (68 loc) · 4.65 KB
/
Generate 5 min table.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
----------------------------------------------------------------------------------
-- Create Table
----------------------------------------------------------------------------------
DROP TABLE [tbl_5min]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [tbl_5min](
[TimeStamp] [datetime] NOT NULL,
[LogTimeFrom] [datetime] NOT NULL,
[LogTimeTo] [datetime] NOT NULL
CONSTRAINT [PK_tbl_5min] PRIMARY KEY CLUSTERED
(
[TimeStamp] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX tbl_5min_idx_LogTimeFrom
ON [tbl_5min] ([LogTimeFrom],[TimeStamp])
INCLUDE ([LogTimeTo])
GO
----------------------------------------------------------------------------------
-- Populate Table
----------------------------------------------------------------------------------
SET NOCOUNT ON
-- TimeStamp -- Goes up every 5 mins
-- LogTimeTo -- start boundary
-- LogTimeFrom -- end boundary
DECLARE @CurrentTimeStamp datetime2
DECLARE @LogTimeFrom datetime2
DECLARE @LogTimeTo datetime2
SELECT @CurrentTimeStamp = '2017-12-31 00:00:00.000'
SET @LogTimeFrom = @CurrentTimeStamp
SELECT @LogTimeTo = DATEADD(MINUTE, 5, @LogTimeFrom)
WHILE @CurrentTimeStamp < CONVERT(datetime2, '2027-12-31 00:00:00.000')
BEGIN
--SELECT @CurrentTimeStamp, @LogTimeFrom, @LogTimeTo
INSERT INTO [tbl_5min]
(
[TimeStamp]
,[LogTimeFrom]
,[LogTimeTo]
)
values
(
@CurrentTimeStamp,
@LogTimeFrom,
@LogTimeTo
)
-- Set CurrentMaxDate to be 5 minutes time
SELECT @CurrentTimeStamp = DATEADD(MINUTE, 5, @CurrentTimeStamp)
IF @CurrentTimeStamp = @LogTimeTo
BEGIN
SET @LogTimeFrom = @CurrentTimeStamp
SELECT @LogTimeTo = DATEADD(MINUTE, 5, @LogTimeFrom)
END
END
----------------------------------------------------------------------------------
-- Test Table
----------------------------------------------------------------------------------
SELECT TOP (100) [TimeStamp]
,[LogTimeFrom]
,[LogTimeTo]
FROM [tbl_5min]
where [TimeStamp] > '2018-06-30 17:30:00.000'
order by [TimeStamp]