How do you time weight average data between two dates for any desired interval in Microsoft SQL Server 2014 Express? - sql

I am trying to write a SQL call that will time weight average data in any desired interval. This meaning i only have to change a few parameters inside the query so the final output will be in Day's, Hours and or minutes between my two desired dates.
I also need it to fill in data between missing intervals like this Example
Actual DataBase Data
Time_Stamp Time_Stamp_ms BPS_FIT0161
2014-07-26 22:32:36 74 164
2014-07-26 22:32:37 71 164
2014-07-26 22:32:38 71 164
2014-07-26 22:32:39 70 162
2014-07-26 22:32:40 71 162
2014-07-26 22:32:41 67 162
2014-07-26 22:32:42 64 165
2014-07-26 22:32:43 63 164
2014-07-26 22:32:44 62 164
2014-07-26 22:32:45 63 163
2014-07-26 22:32:46 59 163
2014-07-26 22:32:47 56 165
2014-07-26 22:32:48 55 167
2014-07-26 22:32:49 54 167
2014-07-26 22:32:50 54 168
2014-07-26 22:32:51 51 168
2014-07-26 22:32:52 47 171
2014-07-26 22:32:53 46 173
2014-07-26 22:32:54 111 177
2014-07-26 22:32:55 42 178
2014-07-26 22:38:56 99 178
2014-07-26 23:24:57 426 178
2014-07-27 00:21:58 854 178
2014-07-27 01:53:09 229 178
2014-07-27 03:30:11 419 178
2014-07-27 05:25:14 56 178
2014-07-27 07:32:16 881 178
2014-07-27 09:48:20 48 178
2014-07-27 12:55:24 286 178
2014-07-27 16:13:28 562 178
2014-07-27 20:10:33 803 178
2014-07-28 00:56:40 26 178
2014-07-28 06:38:47 753 178
2014-07-28 08:38:47 753 178
2014-07-28 09:24:37 219 248
2014-07-28 09:24:38 218 248
2014-07-28 09:24:39 214 226
2014-07-28 09:24:40 212 226
2014-07-28 09:24:41 212 226
2014-07-28 09:24:42 208 224
2014-07-28 09:24:43 207 222
2014-07-28 09:24:44 206 222
2014-07-28 09:24:45 206 222
2014-07-28 10:11:45 604 202
SQL Time Weighted Average (TWA) Hourly query should look something like this:
Date_time BPS_FIT0161 TWA
2014-07-26 22:00:00 177.4342105
2014-07-26 23:00:00 178
2014-07-27 00:00:00 178
2014-07-27 01:00:00 178
2014-07-27 02:00:00 178
2014-07-27 03:00:00 178
2014-07-27 04:00:00 178
2014-07-27 05:00:00 178
2014-07-27 06:00:00 178
2014-07-27 07:00:00 178
2014-07-27 08:00:00 178
2014-07-27 09:00:00 178
2014-07-27 10:00:00 178
2014-07-27 11:00:00 178
2014-07-27 12:00:00 178
2014-07-27 13:00:00 178
2014-07-27 14:00:00 178
2014-07-27 15:00:00 178
2014-07-27 16:00:00 178
2014-07-27 17:00:00 178
2014-07-27 18:00:00 178
2014-07-27 19:00:00 178
2014-07-27 20:00:00 178
2014-07-27 21:00:00 178
2014-07-27 22:00:00 178
2014-07-27 23:00:00 178
2014-07-28 00:00:00 178
2014-07-28 01:00:00 178
2014-07-28 02:00:00 178
2014-07-28 03:00:00 178
2014-07-28 04:00:00 178
2014-07-28 05:00:00 178
2014-07-28 06:00:00 178
2014-07-28 07:00:00 178
2014-07-28 08:00:00 178
2014-07-28 09:00:00 179.5349202
2014-07-28 10:00:00 202.0857852
Thank you so much for your help!
http://sqlfiddle.com/#!6/a8db7/1
End of my question
Beginning of solutions
Using VKP's Solution
SELECT dateadd(hour, datediff(hour, 0, Time_Stamp),0) as Date_Time,
sum(time_stamp_ms * bps_fit0161)/sum(time_stamp_ms) as weighted_avg
FROM BPS
WHERE Time_Stamp BETWEEN CONVERT(DATETIME, '2014-07-26 00:00:00', 102)
AND CONVERT(DATETIME, '2014-07-30 00:00:00', 102)
group by dateadd(hour, datediff(hour, 0, Time_Stamp),0),
dateadd(day,datediff(day, 0, Time_Stamp),0)
order by dateadd(hour, datediff(hour, 0, Time_Stamp),0),
dateadd(day,datediff(day, 0, Time_Stamp),0)
My Query's Result
Date_Time weighted_avg
2014-07-26 00:00:00.000 180
2014-07-26 01:00:00.000 113
2014-07-26 02:00:00.000 147
2014-07-26 03:00:00.000 221
2014-07-26 04:00:00.000 252
2014-07-26 05:00:00.000 379
2014-07-26 06:00:00.000 370
2014-07-26 07:00:00.000 253
2014-07-26 08:00:00.000 125
2014-07-26 09:00:00.000 119
2014-07-26 10:00:00.000 125
2014-07-26 11:00:00.000 117
2014-07-26 12:00:00.000 160
2014-07-26 13:00:00.000 123
2014-07-26 14:00:00.000 86
2014-07-26 15:00:00.000 81
2014-07-26 16:00:00.000 100
2014-07-26 17:00:00.000 108
2014-07-26 18:00:00.000 175
2014-07-26 19:00:00.000 238
2014-07-26 20:00:00.000 211
2014-07-26 21:00:00.000 231
2014-07-26 22:00:00.000 173
2014-07-26 23:00:00.000 178
2014-07-27 00:00:00.000 178
2014-07-27 01:00:00.000 178 <----- Start of missing Data!
2014-07-27 03:00:00.000 178
2014-07-27 05:00:00.000 178
2014-07-27 07:00:00.000 178
2014-07-27 09:00:00.000 178
2014-07-27 12:00:00.000 178
2014-07-27 16:00:00.000 178
2014-07-27 20:00:00.000 178
2014-07-28 00:00:00.000 178
2014-07-28 06:00:00.000 178
2014-07-28 09:00:00.000 160
2014-07-28 10:00:00.000 134
2014-07-28 11:00:00.000 113
2014-07-28 12:00:00.000 136
2014-07-28 13:00:00.000 131
2014-07-28 14:00:00.000 84
2014-07-28 15:00:00.000 102
As you can see I am missing hours due to our database being offline or wireless nodes on my network losing communication. What would you change in the above query to auto fill missing hours with the previous hours data, the same goes for missing data for days.
http://sqlfiddle.com/#!6/a8db7/31/0

declare #s datetime
declare #e datetime
set #s = '2014-07-26 00:00:00'
set #e = '2014-07-30 00:00:00'
;with x(n) as
(
SELECT TOP (DATEDIFF(HOUR, #s, #e) + 1)
rn = ROW_NUMBER() OVER (ORDER BY [object_id])
FROM sys.all_columns ORDER BY [object_id]
)
select DATEADD(HOUR, n-1, #s) as dt into t from x
;with y as (
SELECT
row_number() over(order by t.dt) as rn,
t.dt,
sum(time_stamp_ms * bps_fit0161) / sum(time_stamp_ms) as weighted_avg
FROM BPS
right join t on t.dt = dateadd(hour, datediff(hour, 0, Time_Stamp),0)
group by t.dt,dateadd(hour, datediff(hour, 0, Time_Stamp),0)
)
select y.dt ,
case when y.weighted_avg is null then prev_y.weighted_avg
else y.weighted_avg end as weighted_avg
from y
left join y prev_y on y.rn = prev_y.rn-1
Try this. This groups by the start of any hour till the end of that hour.
Edited: To include all the hours between specified times. This may get you closer to what you are looking for.

Related

Update fields in table based on previous field records

I have a table of records for pay periods with fields WeekStart and WeekEnd populated for ever fiscal year. In my application a user should be able to update the first WeekEnd date for a given fiscal year and that should in turn update subsequent records WeekStart date by adding 1 day to the Previous WeekEnd date and for the same records's WeekEnd date, add 13 days to the new WeekStart date.
This is part of a Stored Procedure written in SQL Server 2016.
UPDATE [dbo].[staffing_BiweeklyPPCopy]
SET
[WeekStart] = DATEADD(DD, 1, LAG([WeekEnd], 1) OVER (ORDER BY [ID])),
[WeekEnd] = DATEADD(DD, 14, LAG([WeekEnd], 1) OVER (ORDER BY [ID]))
WHERE
[FiscalYear] = #fiscalyear
Original Table contents shown below...
ID WeekStart WeekEnd
163 2018-10-01 2018-10-13
164 2018-10-14 2018-10-27
165 2018-10-28 2018-11-10
166 2018-11-11 2018-11-24
167 2018-11-25 2018-12-08
168 2018-12-09 2018-12-22
169 2018-12-23 2019-01-05
170 2019-01-06 2019-01-19
171 2019-01-20 2019-02-02
172 2019-02-03 2019-02-16
173 2019-02-17 2019-03-02
174 2019-03-03 2019-03-16
175 2019-03-17 2019-03-30
176 2019-03-31 2019-04-13
177 2019-04-14 2019-04-27
178 2019-04-28 2019-05-11
179 2019-05-12 2019-05-25
180 2019-05-26 2019-06-08
181 2019-06-09 2019-06-22
182 2019-06-23 2019-07-06
183 2019-07-07 2019-07-20
184 2019-07-21 2019-08-03
185 2019-08-04 2019-08-17
186 2019-08-18 2019-08-31
187 2019-09-01 2019-09-14
188 2019-09-15 2019-09-28
189 2019-09-29 2019-09-30
For example if a user updates the weekend date for record ID 163 to '2018-10-14', the table will update as follows..
ID WeekStart WeekEnd
163 2018-10-01 2018-10-14
164 2018-10-15 2018-10-28
165 2018-10-29 2018-11-11
166 2018-11-12 2018-11-25
167 2018-11-26 2018-12-09
.
.
.
189 2019-09-30 2019-09-30
Thank you in advance.

Finding Last Record Per Group with Multiple Aggregate Functions- SQLite

I have a table ("trds") that contains fields RefDate, Symbol, Timestamp, Sequence, Quantity, Price, SaleCondition, Pid, and Submkt ("select * from trds limit 100"):
RefDate Symbol Timestamp Sequence Quantity Price SaleCondition Pid SubMkt
1 2015-08-03 AAPL 14400002 507 10 121.50 # TI P
2 2015-08-03 AAPL 14400026 1017 100 121.39 T P
3 2015-08-03 AAPL 14400026 1018 520 121.39 T P
4 2015-08-03 AAPL 14400026 1019 100 121.40 T P
5 2015-08-03 AAPL 14400028 1020 100 121.49 T P
6 2015-08-03 AAPL 14400028 1021 2172 121.49 T P
7 2015-08-03 AAPL 14425890 1026 100 121.49 T P
8 2015-08-03 AAPL 14426989 1027 100 121.49 T P
9 2015-08-03 AAPL 14433658 1028 137 121.40 #FT P
10 2015-08-03 AAPL 14628998 1029 200 121.40 T P
11 2015-08-03 AAPL 14637576 1030 5328 121.40 # T P
12 2015-08-03 AAPL 14637576 1031 200 121.45 #FT Q
13 2015-08-03 AAPL 15116858 1032 3040 121.40 T P
14 2015-08-03 AAPL 15129926 1034 1295 121.40 T P
15 2015-08-03 AAPL 15130128 1035 201 121.30 T P
16 2015-08-03 AAPL 15237959 1036 126 121.30 T P
17 2015-08-03 AAPL 15553155 1043 20 121.03 # TI P
18 2015-08-03 AAPL 15646925 1044 80 121.03 # TI P
19 2015-08-03 AAPL 15844129 1050 40 121.22 #FTI Q
20 2015-08-03 AAPL 16908849 1055 190 121.22 #FT Q
21 2015-08-03 AAPL 16998884 1056 260 121.22 #FT Q
22 2015-08-03 AAPL 17682717 1057 10 121.22 #FTI Q
23 2015-08-03 AAPL 17682717 1058 190 121.23 #FT Q
24 2015-08-03 AAPL 17736566 1059 100 121.23 #FT Q
25 2015-08-03 AAPL 18102786 1061 100 121.22 #FT Q
26 2015-08-03 AAPL 18102786 1062 100 121.22 #FT Q
27 2015-08-03 AAPL 18103386 1063 700 121.23 #FT Q
28 2015-08-03 AAPL 19170799 1064 200 121.22 #FT P
29 2015-08-03 AAPL 19171001 1065 40 121.22 #FTI P
30 2015-08-03 AAPL 19557088 1068 500 121.21 T P
31 2015-08-03 AAPL 19995780 1072 50 121.00 #FTI P
32 2015-08-03 AAPL 19995780 1073 450 121.00 #FT P
33 2015-08-03 AAPL 20110219 1074 100 121.00 #FT P
34 2015-08-03 AAPL 20131347 1075 100 121.00 #FT P
35 2015-08-03 AAPL 20288087 1076 50 121.01 #FTI P
36 2015-08-03 AAPL 20288282 1077 250 121.00 #FT P
37 2015-08-03 AAPL 20776851 1081 200 121.22 T P
38 2015-08-03 AAPL 20777052 1082 300 121.22 T P
39 2015-08-03 AAPL 21416707 1084 200 121.22 T P
40 2015-08-03 AAPL 21416907 1085 300 121.22 T P
41 2015-08-03 AAPL 21740935 1086 200 121.22 #FT P
42 2015-08-03 AAPL 21741136 1087 33 121.22 #FTI P
43 2015-08-03 AAPL 22289290 1088 50 121.22 # TI P
44 2015-08-03 AAPL 22302889 1089 150 121.22 T P
45 2015-08-03 AAPL 22303093 1090 350 121.22 T P
46 2015-08-03 AAPL 22324450 1091 100 121.22 T P
47 2015-08-03 AAPL 22325298 1092 100 121.22 T P
48 2015-08-03 AAPL 22489509 1093 200 121.22 T P
49 2015-08-03 AAPL 22489711 1094 300 121.22 T P
50 2015-08-03 AAPL 22651416 1096 200 121.22 #FT P
51 2015-08-03 AAPL 22656656 1097 77 121.22 # TI P
52 2015-08-03 AAPL 22656857 1098 10 121.23 # TI P
53 2015-08-03 AAPL 22762084 1099 87 121.37 #FTI Q
54 2015-08-03 AAPL 22762084 1100 413 121.38 #FT Q
55 2015-08-03 AAPL 22956526 1103 20 121.20 # TI P
56 2015-08-03 AAPL 22956727 1104 180 121.18 T P
57 2015-08-03 AAPL 23254802 1105 36 121.27 # TI P
58 2015-08-03 AAPL 24077374 1110 144 121.27 T P
59 2015-08-03 AAPL 24077374 1111 50 121.29 # TI P
60 2015-08-03 AAPL 24077374 1112 306 121.30 T P
61 2015-08-03 AAPL 24121832 1113 2 121.30 # TI P
62 2015-08-03 AAPL 24164641 1116 1000 121.30 T P
63 2015-08-03 AAPL 24168414 1117 600 121.30 T P
64 2015-08-03 AAPL 24192881 1120 92 121.30 # TI P
65 2015-08-03 AAPL 24192882 1121 408 121.38 #FT Q
66 2015-08-03 AAPL 24331392 1122 400 121.45 # T P
67 2015-08-03 AAPL 24331392 1123 100 121.38 #FT Q
68 2015-08-03 AAPL 24336664 1124 79 121.38 # TI P
69 2015-08-03 AAPL 24464675 1125 1000 121.40 T P
70 2015-08-03 AAPL 24587817 1126 496 121.38 T P
71 2015-08-03 AAPL 24588021 1127 504 121.39 T P
72 2015-08-03 AAPL 24600057 1128 500 121.38 T P
73 2015-08-03 AAPL 24667891 1130 500 121.37 T P
74 2015-08-03 AAPL 24723357 1131 50 121.39 # TI P
75 2015-08-03 AAPL 24778654 1132 1000 121.39 T P
76 2015-08-03 AAPL 24908008 1133 200 121.39 T P
77 2015-08-03 AAPL 24908210 1134 300 121.40 T P
78 2015-08-03 AAPL 24912914 1135 1500 121.40 T P
79 2015-08-03 AAPL 25011487 1136 500 121.40 T P
80 2015-08-03 AAPL 25018982 1137 100 121.40 T P
81 2015-08-03 AAPL 25023375 1138 21 121.40 # TI P
82 2015-08-03 AAPL 25064872 1139 600 121.40 T P
83 2015-08-03 AAPL 25111247 1140 500 121.40 T P
84 2015-08-03 AAPL 25112575 1141 200 121.40 T P
85 2015-08-03 AAPL 25139881 1142 200 121.40 T P
86 2015-08-03 AAPL 25140659 1143 79 121.40 # TI P
87 2015-08-03 AAPL 25140869 1144 421 121.45 T P
88 2015-08-03 AAPL 25219916 1146 200 121.45 T P
89 2015-08-03 AAPL 25229789 1147 50 121.36 #FTI P
90 2015-08-03 AAPL 25229988 1148 29 121.35 #FTI P
91 2015-08-03 AAPL 25290394 1160 200 121.43 T P
92 2015-08-03 AAPL 25392283 1168 30 121.40 # TI P
93 2015-08-03 AAPL 25421012 1169 300 121.40 T P
94 2015-08-03 AAPL 25755052 1173 2 121.45 # TI P
95 2015-08-03 AAPL 25763189 1174 60 121.44 # TI P
96 2015-08-03 AAPL 25942067 1182 200 121.46 #FT P
97 2015-08-03 AAPL 25942068 1183 100 121.47 #FT P
98 2015-08-03 AAPL 25942068 1184 100 121.47 #FT P
99 2015-08-03 AAPL 25942070 1185 100 121.47 #FT P
100 2015-08-03 AAPL 25948942 1186 1000 121.48 T
I need to create a query that groups together all trades in five minute intervals and finds the Min/Max/Avg and Last Price of each group. "Timestamp" is milliseconds since midnight (so 9:30 is 34200000), and I only want to include between 9:30 and 4:00. I created timebucket by dividing by five minute intervals (300,000 ms): cast(timestamp/300e3) as int) as timebucket.
The end result should look like this:
I can produce code to get the Min/Max/Avg price that looks like this:
dbGetQuery(nqdb, statement = "select t.RefDate, t.Symbol, cast(t.Timestamp/300e3 as int) as timeBucket,
Min(time(t.timestamp/1000, 'unixepoch')) as startTime, Max(t.Price) as MaxPrice,
Min(t.Price) as MinPrice, Avg(t.Price) as AvgPrice
from trds t
where (t.Timestamp between 34200000 and 57600000) and
SaleCondition not glob '*[CGIHMNPQRTUVWZ47]*' group by timeBucket, symbol order by symbol ASC")
This is somewhat close, although the StartTime has seconds in the HH:MM:SS field when it shouldn't, and I've tried everything from sub-queries to sub-selects and searched through "greatest-n-per-group" forums to get the "LastPrice" to no avail. Here's the output:
RefDate Symbol timeBucket startTime MaxPrice MinPrice AvgPrice
1 2015-08-03 AAPL 114 09:30:00 121.5000 121.0500 121.22574
2 2015-08-03 AAPL 115 09:35:00 122.4700 121.2200 121.86515
3 2015-08-03 AAPL 116 09:40:00 122.5700 122.1000 122.37850
4 2015-08-03 AAPL 117 09:45:00 122.2000 121.8901 122.00520
5 2015-08-03 AAPL 118 09:50:00 122.2900 122.0000 122.10803
6 2015-08-03 AAPL 119 09:55:00 122.2100 121.7000 122.02053
7 2015-08-03 AAPL 120 10:00:00 121.8900 121.4100 121.73007
8 2015-08-03 AAPL 121 10:05:00 121.5500 121.2500 121.38513
9 2015-08-03 AAPL 122 10:10:00 121.6200 121.2550 121.48453
10 2015-08-03 AAPL 123 10:15:00 121.5200 121.2700 121.38975
11 2015-08-03 AAPL 124 10:20:00 121.3400 121.1500 121.25174
12 2015-08-03 AAPL 125 10:25:00 121.1600 120.7750 120.92567
13 2015-08-03 AAPL 126 10:30:00 121.0400 120.8100 120.90262
14 2015-08-03 AAPL 127 10:35:01 121.0000 120.7800 120.88182
15 2015-08-03 AAPL 128 10:40:00 121.0000 120.8600 120.91147
16 2015-08-03 AAPL 129 10:45:00 120.9083 120.7330 120.81935
17 2015-08-03 AAPL 130 10:50:00 120.8400 120.5500 120.71769
18 2015-08-03 AAPL 131 10:55:00 120.7200 120.5300 120.62324
19 2015-08-03 AAPL 132 11:00:00 120.8200 120.6500 120.71650
20 2015-08-03 AAPL 133 11:05:00 120.9900 120.6500 120.83935
21 2015-08-03 AAPL 134 11:10:00 121.1800 120.9100 121.02811
22 2015-08-03 AAPL 135 11:15:00 121.1700 120.9700 121.05849
23 2015-08-03 AAPL 136 11:20:01 121.1500 121.0480 121.09072
24 2015-08-03 AAPL 137 11:25:00 121.1500 120.9500 121.04811
25 2015-08-03 AAPL 138 11:30:00 121.1200 120.9200 121.02047
26 2015-08-03 AAPL 139 11:35:00 120.9900 120.6700 120.82496
27 2015-08-03 AAPL 140 11:40:00 120.8400 120.5600 120.68603
28 2015-08-03 AAPL 141 11:45:00 120.8600 120.6600 120.75718
29 2015-08-03 AAPL 142 11:50:00 120.6800 120.3300 120.46856
30 2015-08-03 AAPL 143 11:55:00 120.5155 120.4100 120.45356
31 2015-08-03 AAPL 144 12:00:00 120.5500 120.2200 120.32099
32 2015-08-03 AAPL 145 12:05:00 120.3000 120.1100 120.20832
33 2015-08-03 AAPL 146 12:10:00 120.1734 119.9000 120.02722
34 2015-08-03 AAPL 147 12:15:00 120.0600 119.8700 119.96583
35 2015-08-03 AAPL 148 12:20:00 119.9900 119.8100 119.89728
36 2015-08-03 AAPL 149 12:25:01 120.2200 119.8900 120.03213
37 2015-08-03 AAPL 150 12:30:00 120.1800 119.9200 120.03840
38 2015-08-03 AAPL 151 12:35:00 119.9756 119.7700 119.87204
39 2015-08-03 AAPL 152 12:40:00 119.9000 119.7500 119.82387
40 2015-08-03 AAPL 153 12:45:00 119.8353 118.7000 119.16163
41 2015-08-03 AAPL 154 12:50:00 118.9890 118.2600 118.66432
42 2015-08-03 AAPL 155 12:55:00 118.5000 117.5200 117.95536
43 2015-08-03 AAPL 156 13:00:00 118.4367 117.9500 118.15729
44 2015-08-03 AAPL 157 13:05:00 118.4500 117.7101 118.08337
45 2015-08-03 AAPL 158 13:10:00 118.1200 117.6700 117.87640
46 2015-08-03 AAPL 159 13:15:00 118.0100 117.6601 117.82184
47 2015-08-03 AAPL 160 13:20:00 118.1700 117.6100 117.98232
48 2015-08-03 AAPL 161 13:25:00 118.4400 118.0200 118.27480
49 2015-08-03 AAPL 162 13:30:00 118.4500 118.2000 118.33434
50 2015-08-03 AAPL 163 13:35:00 118.5500 118.3000 118.45115
51 2015-08-03 AAPL 164 13:40:00 118.6400 118.3200 118.50168
52 2015-08-03 AAPL 165 13:45:00 118.6800 118.4400 118.54192
53 2015-08-03 AAPL 166 13:50:00 118.8300 118.6000 118.74204
54 2015-08-03 AAPL 167 13:55:00 118.8400 118.6500 118.73936
55 2015-08-03 AAPL 168 14:00:00 118.7300 118.5200 118.60532
56 2015-08-03 AAPL 169 14:05:00 118.6500 118.4300 118.50543
57 2015-08-03 AAPL 170 14:10:00 118.5800 118.4300 118.49353
58 2015-08-03 AAPL 171 14:15:01 118.7100 118.4600 118.58022
59 2015-08-03 AAPL 172 14:20:00 118.5600 118.4000 118.48883
60 2015-08-03 AAPL 173 14:25:00 118.6500 118.3300 118.49131
61 2015-08-03 AAPL 174 14:30:00 118.6675 118.4900 118.56914
62 2015-08-03 AAPL 175 14:35:00 118.6500 118.4700 118.55887
63 2015-08-03 AAPL 176 14:40:00 118.8100 118.5600 118.69735
64 2015-08-03 AAPL 177 14:45:00 118.8000 118.6200 118.72279
65 2015-08-03 AAPL 178 14:50:00 118.7800 118.6200 118.71055
66 2015-08-03 AAPL 179 14:55:00 118.7500 118.5700 118.65656
67 2015-08-03 AAPL 180 15:00:00 118.8700 118.6500 118.72095
68 2015-08-03 AAPL 181 15:05:00 118.8700 118.6900 118.78130
69 2015-08-03 AAPL 182 15:10:00 118.7200 118.4600 118.61231
70 2015-08-03 AAPL 183 15:15:01 118.6000 118.3000 118.42988
71 2015-08-03 AAPL 184 15:20:03 118.4000 118.0700 118.23301
72 2015-08-03 AAPL 185 15:25:00 118.1000 117.8600 117.99564
73 2015-08-03 AAPL 186 15:30:00 118.1900 117.9600 118.08736
74 2015-08-03 AAPL 187 15:35:00 118.1700 117.8800 117.99449
75 2015-08-03 AAPL 188 15:40:00 118.2700 118.1000 118.16962
76 2015-08-03 AAPL 189 15:45:00 118.2000 117.9600 118.08088
77 2015-08-03 AAPL 190 15:50:00 118.1700 118.0000 118.07813
78 2015-08-03 AAPL 191 15:55:00 118.4500 118.0300 118.23987
79 2015-08-03 AMZN 114 09:30:00 538.5500 536.3200 537.61255
80 2015-08-03 AMZN 115 09:35:04 537.4600 535.7700 536.42177
81 2015-08-03 AMZN 116 09:40:01 538.3500 536.4000 537.68861
82 2015-08-03 AMZN 117 09:45:00 539.3500 536.2800 537.56732
83 2015-08-03 AMZN 118 09:50:00 539.9500 538.7100 539.45455
84 2015-08-03 AMZN 119 09:55:03 540.4400 538.8100 539.66087
85 2015-08-03 AMZN 120 10:00:00 540.2600 537.7100 539.01779
86 2015-08-03 AMZN 121 10:05:00 538.4050 536.5500 537.48823
87 2015-08-03 AMZN 122 10:10:03 537.6800 536.8000 537.21956
88 2015-08-03 AMZN 123 10:15:00 536.8000 534.1500 535.24578
89 2015-08-03 AMZN 124 10:20:01 535.0500 533.7700 534.51831
90 2015-08-03 AMZN 125 10:25:04 535.3900 534.1200 534.74367
91 2015-08-03 AMZN 126 10:30:13 534.9700 534.0110 534.64140
92 2015-08-03 AMZN 127 10:35:04 535.3500 534.5001 534.92485
93 2015-08-03 AMZN 128 10:40:04 535.1600 534.1000 534.57402
94 2015-08-03 AMZN 129 10:45:02 536.0000 534.5100 535.26481
95 2015-08-03 AMZN 130 10:50:00 536.3500 535.4500 535.93280
96 2015-08-03 AMZN 131 10:55:02 536.6500 535.2600 535.96416
97 2015-08-03 AMZN 132 11:00:01 536.6000 535.6800 536.24564
98 2015-08-03 AMZN 133 11:05:10 537.8900 536.4800 537.23168
99 2015-08-03 AMZN 134 11:10:00 538.5200 537.3500 537.94708
100 2015-08-03 AMZN 135 11:15:02 537.9400 537.3500 537.58755
101 2015-08-03 AMZN 136 11:20:05 537.9050 536.8600 537.31598
I'm guessing there's a more elegant solution than the Min(time) work around I used. But the primary question is how to find the LastPrice in each five minute interval?
Update- This is a separate query I did that produces the last price per five minute group, but without Min(Price), Max(Price), and Avg(Price). I do not know how to combine all of them in one query.
dbGetQuery(nqdb, statement = "select t.RefDate, t.Symbol, cast(t.Timestamp/300e3 as int) as timeBucket,
time(t.timestamp/1000, 'unixepoch') as startTime,
t.price as LastPrice from trds t
where (t.Timestamp between 34200000 and 57600000) and
SaleCondition not glob '*[CGIHMNPQRTUVWZ47]*' group by timeBucket, symbol order by symbol ASC")
RefDate Symbol timeBucket startTime LastPrice
1 2015-08-03 AAPL 114 09:34:00 121.2300
2 2015-08-03 AAPL 115 09:39:00 122.4400
3 2015-08-03 AAPL 116 09:44:00 122.1800
4 2015-08-03 AAPL 117 09:49:00 122.0500
5 2015-08-03 AAPL 118 09:54:00 122.0700
6 2015-08-03 AAPL 119 09:59:00 121.7700
7 2015-08-03 AAPL 120 10:04:00 121.5400
8 2015-08-03 AAPL 121 10:09:00 121.2600
9 2015-08-03 AAPL 122 10:14:00 121.5140
10 2015-08-03 AAPL 123 10:19:00 121.2700
11 2015-08-03 AAPL 124 10:24:00 121.1600
12 2015-08-03 AAPL 125 10:29:00 121.0200
13 2015-08-03 AAPL 126 10:34:00 120.9200
14 2015-08-03 AAPL 127 10:39:00 120.9900
15 2015-08-03 AAPL 128 10:44:00 120.8600
16 2015-08-03 AAPL 129 10:49:00 120.7800
17 2015-08-03 AAPL 130 10:54:00 120.6200
18 2015-08-03 AAPL 131 10:59:00 120.6500
19 2015-08-03 AAPL 132 11:04:00 120.6900
20 2015-08-03 AAPL 133 11:09:00 120.9600
21 2015-08-03 AAPL 134 11:14:00 121.0237
22 2015-08-03 AAPL 135 11:19:00 121.0899
23 2015-08-03 AAPL 136 11:24:00 121.1010
24 2015-08-03 AAPL 137 11:29:00 120.9954
25 2015-08-03 AAPL 138 11:34:00 120.9895
26 2015-08-03 AAPL 139 11:39:00 120.6750
27 2015-08-03 AAPL 140 11:44:00 120.8200
28 2015-08-03 AAPL 141 11:49:00 120.6750
29 2015-08-03 AAPL 142 11:54:00 120.4500
30 2015-08-03 AAPL 143 11:59:00 120.4900
31 2015-08-03 AAPL 144 12:04:00 120.2449
32 2015-08-03 AAPL 145 12:09:00 120.1600
33 2015-08-03 AAPL 146 12:14:00 120.0200
34 2015-08-03 AAPL 147 12:19:00 119.8700
35 2015-08-03 AAPL 148 12:24:00 119.9400
36 2015-08-03 AAPL 149 12:29:00 120.1770
37 2015-08-03 AAPL 150 12:34:00 119.9505
38 2015-08-03 AAPL 151 12:39:00 119.8400
39 2015-08-03 AAPL 152 12:44:00 119.8400
40 2015-08-03 AAPL 153 12:49:00 118.9400
41 2015-08-03 AAPL 154 12:54:00 118.4900
42 2015-08-03 AAPL 155 12:59:00 118.0000
43 2015-08-03 AAPL 156 13:04:00 118.4300
44 2015-08-03 AAPL 157 13:09:00 117.7286
45 2015-08-03 AAPL 158 13:14:00 117.9173
46 2015-08-03 AAPL 159 13:19:00 117.6700
47 2015-08-03 AAPL 160 13:24:00 118.1200
48 2015-08-03 AAPL 161 13:29:00 118.3601
49 2015-08-03 AAPL 162 13:34:00 118.4400
50 2015-08-03 AAPL 163 13:39:00 118.3000
Update: per #Parfait-on the timestamp issue, it mostly works, however one of the the output is off with one Symbol. Also still doesn't solve the problem of finding the Last Price per five minute group along with min/max/avg:
157 2015-08-03 DAVE 114 09:30:00 17.9600 17.9300 17.94500
158 2015-08-03 DAVE 115 09:37:00 17.9994 17.9100 17.95470
159 2015-08-03 DAVE 116 09:40:00 17.8700 17.8700 17.87000
160 2015-08-03 DAVE 118 09:50:00 17.7350 17.6950 17.71500
161 2015-08-03 DAVE 120 10:04:00 17.6900 17.6700 17.68000
162 2015-08-03 DAVE 121 10:08:00 17.5600 17.5600 17.56000
163 2015-08-03 DAVE 122 10:14:00 17.5600 17.5600 17.56000
164 2015-08-03 DAVE 124 10:24:00 17.6500 17.6500 17.65000
165 2015-08-03 DAVE 126 10:30:00 17.6000 17.5200 17.56000
166 2015-08-03 DAVE 127 10:39:00 17.4800 17.4800 17.48000
167 2015-08-03 DAVE 128 10:40:00 17.6100 17.4900 17.55000
168 2015-08-03 DAVE 129 10:47:00 17.4400 17.4400 17.44000
169 2015-08-03 DAVE 130 10:54:00 17.5600 17.5600 17.56000
170 2015-08-03 DAVE 131 10:55:00 17.4300 17.3800 17.40500
171 2015-08-03 DAVE 132 11:00:00 17.3400 17.2620 17.30720
172 2015-08-03 DAVE 133 11:09:00 17.2800 17.2700 17.27500
173 2015-08-03 DAVE 135 11:17:00 17.2700 17.2300 17.25000
174 2015-08-03 DAVE 136 11:23:00 17.3200 17.3000 17.31000
175 2015-08-03 DAVE 137 11:25:00 17.4000 17.3750 17.38875
176 2015-08-03 DAVE 139 11:39:00 17.4400 17.4400 17.44000
177 2015-08-03 DAVE 141 11:49:00 17.4200 17.4200 17.42000
178 2015-08-03 DAVE 142 11:51:00 17.4200 17.4200 17.42000
179 2015-08-03 DAVE 143 11:59:00 17.3900 17.3900 17.39000
180 2015-08-03 DAVE 144 12:01:00 17.4350 17.3600 17.39750
181 2015-08-03 DAVE 146 12:13:00 17.3700 17.3600 17.36500
182 2015-08-03 DAVE 147 12:17:00 17.3600 17.3500 17.35333
183 2015-08-03 DAVE 148 12:20:00 17.4000 17.3700 17.38500
184 2015-08-03 DAVE 149 12:25:00 17.3700 17.3700 17.37000
185 2015-08-03 DAVE 150 12:31:00 17.3300 17.3300 17.33000
186 2015-08-03 DAVE 153 12:49:00 17.3800 17.3800 17.38000
187 2015-08-03 DAVE 154 12:50:00 17.3900 17.3900 17.39000
188 2015-08-03 DAVE 155 12:55:00 17.3300 17.3300 17.33000
189 2015-08-03 DAVE 158 13:12:00 17.3400 17.3100 17.32500
190 2015-08-03 DAVE 159 13:15:00 17.3600 17.3400 17.35000
191 2015-08-03 DAVE 160 13:22:00 17.3700 17.3700 17.37000
192 2015-08-03 DAVE 162 13:34:00 17.3504 17.3504 17.35040
193 2015-08-03 DAVE 163 13:35:00 17.3300 17.3100 17.32000
194 2015-08-03 DAVE 165 13:49:00 17.3200 17.3200 17.32000
195 2015-08-03 DAVE 171 14:15:00 17.3500 17.2700 17.31563
196 2015-08-03 DAVE 172 14:20:00 17.4000 17.3500 17.36667
197 2015-08-03 DAVE 175 14:37:00 17.3900 17.3900 17.39000
198 2015-08-03 DAVE 176 14:40:00 17.4000 17.3500 17.37125
199 2015-08-03 DAVE 177 14:45:00 17.4100 17.3500 17.38300
200 2015-08-03 DAVE 178 14:51:00 17.4500 17.4500 17.45000
201 2015-08-03 DAVE 181 15:05:00 17.5800 17.5300 17.55700
202 2015-08-03 DAVE 182 15:10:00 17.5600 17.5200 17.54223
203 2015-08-03 DAVE 184 15:23:00 17.6900 17.6200 17.65500
204 2015-08-03 DAVE 186 15:30:00 17.6900 17.6900 17.69000
205 2015-08-03 DAVE 189 15:46:00 17.7500 17.7200 17.74000
Here's the time in seconds for the problem Symbol "DAVE":
RefDate Symbol timeBucket startTime MaxPrice MinPrice AvgPrice
1 2015-08-03 DAVE 114 34200708 17.9600 17.9300 17.94500
2 2015-08-03 DAVE 115 34628936 17.9994 17.9100 17.95470
3 2015-08-03 DAVE 116 34807939 17.8700 17.8700 17.87000
4 2015-08-03 DAVE 118 35415342 17.7350 17.6950 17.71500
5 2015-08-03 DAVE 120 36250083 17.6900 17.6700 17.68000
6 2015-08-03 DAVE 121 36528404 17.5600 17.5600 17.56000
7 2015-08-03 DAVE 122 36850085 17.5600 17.5600 17.56000
8 2015-08-03 DAVE 124 37450198 17.6500 17.6500 17.65000
9 2015-08-03 DAVE 126 37892189 17.6000 17.5200 17.56000
10 2015-08-03 DAVE 127 38389126 17.4800 17.4800 17.48000
11 2015-08-03 DAVE 128 38400053 17.6100 17.4900 17.55000
12 2015-08-03 DAVE 129 38861146 17.4400 17.4400 17.44000
13 2015-08-03 DAVE 130 39250319 17.5600 17.5600 17.56000
14 2015-08-03 DAVE 131 39301007 17.4300 17.3800 17.40500
15 2015-08-03 DAVE 132 39704588 17.3400 17.2620 17.30720
16 2015-08-03 DAVE 133 40150085 17.2800 17.2700 17.27500
17 2015-08-03 DAVE 135 40668185 17.2700 17.2300 17.25000
18 2015-08-03 DAVE 136 41026622 17.3200 17.3000 17.31000
19 2015-08-03 DAVE 137 41125827 17.4000 17.3750 17.38875
20 2015-08-03 DAVE 139 41950078 17.4400 17.4400 17.44000
21 2015-08-03 DAVE 141 42550193 17.4200 17.4200 17.42000
22 2015-08-03 DAVE 142 42704053 17.4200 17.4200 17.42000
23 2015-08-03 DAVE 143 43150086 17.3900 17.3900 17.39000
24 2015-08-03 DAVE 144 43439310 17.4350 17.3600 17.39750
25 2015-08-03 DAVE 146 44086864 17.3700 17.3600 17.36500
26 2015-08-03 DAVE 147 44250485 17.3600 17.3500 17.35333
27 2015-08-03 DAVE 148 44416744 17.4000 17.3700 17.38500
28 2015-08-03 DAVE 149 44748677 17.3700 17.3700 17.37000
29 2015-08-03 DAVE 150 45087379 17.3300 17.3300 17.33000
30 2015-08-03 DAVE 153 46149940 17.3800 17.3800 17.38000
31 2015-08-03 DAVE 154 46236704 17.3900 17.3900 17.39000
32 2015-08-03 DAVE 155 46513141 17.3300 17.3300 17.33000
33 2015-08-03 DAVE 158 47589401 17.3400 17.3100 17.32500
34 2015-08-03 DAVE 159 47706529 17.3600 17.3400 17.35000
35 2015-08-03 DAVE 160 48134567 17.3700 17.3700 17.37000
36 2015-08-03 DAVE 162 48856648 17.3504 17.3504 17.35040
37 2015-08-03 DAVE 163 48943090 17.3300 17.3100 17.32000
38 2015-08-03 DAVE 165 49755204 17.3200 17.3200 17.32000
39 2015-08-03 DAVE 171 51379248 17.3500 17.2700 17.31563
40 2015-08-03 DAVE 172 51622647 17.4000 17.3500 17.36667
41 2015-08-03 DAVE 175 52636324 17.3900 17.3900 17.39000
42 2015-08-03 DAVE 176 52921459 17.4000 17.3500 17.37125
43 2015-08-03 DAVE 177 53132520 17.4100 17.3500 17.38300
44 2015-08-03 DAVE 178 53516691 17.4500 17.4500 17.45000
45 2015-08-03 DAVE 181 54350810 17.5800 17.5300 17.55700
46 2015-08-03 DAVE 182 54749749 17.5600 17.5200 17.54223
47 2015-08-03 DAVE 184 55476188 17.6900 17.6200 17.65500
48 2015-08-03 DAVE 186 55804762 17.6900 17.6900 17.69000
49 2015-08-03 DAVE 189 56789594 17.7500 17.7200 17.74000
50 2015-08-03 DAVE 190 57274050 17.7800 17.6600 17.73333
51 2015-08-03 DAVE 191 57557752 17.7000 17.6300 17.66265
Consider subtracting its remainder using modulo operator, %, for nearest minute (i.e., 60 seconds):
Min(time(t.timestamp/1000 - (t.timestamp/1000) % 60, 'unixepoch')) as startTime
Also, to find first or last price run multiple aggregations using timestamp then left join the aggregates to a main query. Below uses CTE which should be available with latest SQLite version. Wrap entire query in an R string and it should return the last SELECT resultset as data frame.
Below uses a new five_min_grp and runs a self join of aggregate query but joined on different time stamps with the main unit level query.
with unit as
(select t.RefDate, t.Symbol, cast(t.Timestamp/300e3 as int) as timeBucket,
time(t.timestamp/1000 - (t.timestamp/1000) % 300, 'unixepoch') as five_min_grp,
time(t.timestamp/1000 - (t.timestamp/1000) % 60, 'unixepoch') as startTime,
t.price as Price
from trds t
where (t.Timestamp between 34200000 and 57600000)
and (t.SaleCondition not glob '*[CGIHMNPQRTUVWZ47]*')
),
agg as
(select u.RefDate, u.Symbol, u.five_min_grp,
min(u.startTime) as first_startTime,
max(u.startTime) as last_startTime
from unit u
group by u.RefDate, u.Symbol, u.five_min_grp
),
select u.RefDate, u.Symbol, u.five_min_grp,
avg(u.price) as AvgPrice,
min(u.price) as MinPrice,
max(u.price) as MaxPrice,
min(case when first.first_startTime is not null
then u.price
else null
end) as FirstPrice,
max(case when last.last_startTime is not null
then u.price
else null
end) as LastPrice
from unit u
left join agg first
on first.RefDate = u.RefDate
and first.Symbol = u.Symbol
and first.five_min_grp = u.five_min_grp
and first.first_startTime = u.startTime
left join agg last
on last.RefDate = u.RefDate
and last.Symbol = u.Symbol
and last.five_min_grp = u.five_min_grp
and last.last_startTime = u.startTime
group by u.RefDate, u.Symbol, u.five_min_grp
order by u.symbol
DB Fiddle Demo - does not use CTEs since its SQLite version is dated

MSSQL MAX returns all results?

I have tried the following query to return the highest P.Maxvalue for each ME.Name from the last day between 06:00 and 18:00:
SELECT MAX(P.MaxValue) AS Value,P.DateTime,ME.Name AS ID
FROM vManagedEntity AS ME INNER JOIN
Perf.vPerfHourly AS P ON ME.ManagedEntityRowId = P.ManagedEntityRowId INNER JOIN
vPerformanceRuleInstance AS PRI ON P.PerformanceRuleInstanceRowId = PRI.PerformanceRuleInstanceRowId INNER JOIN
vPerformanceRule AS PR ON PRI.RuleRowId = PR.RuleRowId
WHERE (ME.ManagedEntityTypeRowId = 2546) AND (pr.ObjectName = 'VMGuest-cpu') AND (pr.CounterName LIKE 'cpuUsageMHz') AND (CAST(p.DateTime as time) >= '06:00:00' AND CAST(p.DateTime as time) <='18:00:00') AND (p.DateTime > DATEADD(day, - 1, getutcdate()))
group by ME.Name,P.DateTime
ORDER by id
but it seems to return each MaxValue for each ID instead of the highest?
like:
Value DateTime ID
55 2018-02-19 12:00:00.000 bob:vm-100736
51 2018-02-19 13:00:00.000 bob:vm-100736
53 2018-02-19 14:00:00.000 bob:vm-100736
52 2018-02-19 15:00:00.000 bob:vm-100736
52 2018-02-19 16:00:00.000 bob:vm-100736
51 2018-02-19 17:00:00.000 bob:vm-100736
54 2018-02-19 18:00:00.000 bob:vm-100736
51 2018-02-20 06:00:00.000 bob:vm-100736
51 2018-02-20 07:00:00.000 bob:vm-100736
53 2018-02-20 08:00:00.000 bob:vm-100736
52 2018-02-20 09:00:00.000 bob:vm-100736
78 2018-02-19 12:00:00.000 bob:vm-101
82 2018-02-19 13:00:00.000 bob:vm-101
79 2018-02-19 14:00:00.000 bob:vm-101
78 2018-02-19 15:00:00.000 bob:vm-101
79 2018-02-19 16:00:00.000 bob:vm-101
77 2018-02-19 17:00:00.000 bob:vm-101
82 2018-02-19 18:00:00.000 bob:vm-101
82 2018-02-20 06:00:00.000 bob:vm-101
79 2018-02-20 07:00:00.000 bob:vm-101
81 2018-02-20 08:00:00.000 bob:vm-101
82 2018-02-20 09:00:00.000 bob:vm-101
155 2018-02-19 12:00:00.000 bob:vm-104432
there is one value per hour for each id hence twelve results for each id
does MAX not work in this way i want ?
Thanks
expected view like this :
Value DateTime ID
55 2018-02-19 12:00:00.000 bob:vm-100736
82 2018-02-19 13:00:00.000 bob:vm-101
etc
If you're using group by on datetime and id, you'll get all datetimes and all ids, it's that simple.
If you don't need exact time, you can group by date only:
SELECT MAX(P.MaxValue) AS Value, cast(P.DateTime as date) as dat, ME.Name AS ID
...
group by ME.Name, cast(P.DateTime as date)
Or if you do, you may use not exists clause instead of group by.

Getting a cumulative number of records within a day

I am trying to get a cumulative sum of records within a time period of a day. Below is a current sample of my data.
DT No_of_records
2017-05-01 00:00:00.000 241
2017-05-01 04:00:00.000 601
2017-05-01 08:00:00.000 207
2017-05-01 12:00:00.000 468
2017-05-01 16:00:00.000 110
2017-05-01 20:00:00.000 450
2017-05-02 00:00:00.000 151
2017-05-02 04:00:00.000 621
2017-05-02 08:00:00.000 179
2017-05-02 12:00:00.000 163
2017-05-02 16:00:00.000 579
2017-05-02 20:00:00.000 299
I am trying to sum up the number of records until the day changes in another column. My desired output is below.
DT No_of_records cumulative
2017-05-01 00:00:00.000 241 241
2017-05-01 04:00:00.000 601 842
2017-05-01 08:00:00.000 207 1049
2017-05-01 12:00:00.000 468 1517
2017-05-01 16:00:00.000 110 1627
2017-05-01 20:00:00.000 450 2077
2017-05-02 00:00:00.000 151 151
2017-05-02 04:00:00.000 621 772
2017-05-02 08:00:00.000 179 951
2017-05-02 12:00:00.000 163 1114
2017-05-02 16:00:00.000 579 1693
2017-05-02 20:00:00.000 299 1992
Do any of you have ideas on how to get the cumulative column?
If 2012+ you can use with window function sum() over
Select *
,cumulative = sum(No_of_records) over (Partition by cast(DT as date) Order by DT)
From YourTable
You can do this with a windowed SUM():
Select DT, No_of_records,
Sum(No_of_records) Over (Partition By Convert(Date, DT) Order By DT) As cumulative
From YourTable
For older version use CROSS APPLY or Correlated sub-query
SELECT DT,
No_of_records,
cs.cumulative
FROM YourTable a
CROSS apply(SELECT Sum(No_of_records)
FROM YourTable b
WHERE Cast(a.DT AS DATE) = Cast(b.DT AS DATE)
AND a.DT >= b.DT) cs (cumulative)
Rextester Demo

SQL to Find records where result is in range between two columns, but check multiple rows

I'm working with two datasets, one is a more detailed view of clock punch activity, and another is a summation of entire "shifts" that these clock punches make up. What we are trying to accomplish, is to return a record where the PAYCODEID is equal to '7' or '8' (these are meal break codes), and if it falls within the range of the last two columns in the entire shift table just below:
ClockEventShifts:
ShiftID EMPID Clockin MealRangeMax
1 00001280687 2014-02-16 08:00:00.000 2014-02-16 14:00:00.000
6 00001280687 2014-02-17 16:00:00.000 2014-02-17 22:00:00.000
There are a few key possibilities for the more detailed view of clock punches:
ClockEvent A:
EVENTID EMPID CLOCKIN CLOCKOUT PAYCODEID
228 00001280687 2014-02-16 08:00:00.000 2014-02-16 12:00:00.000 20
234 00001280687 2014-02-16 12:00:00.000 2014-02-16 13:00:00.000 8
235 00001280687 2014-02-16 13:00:00.000 2014-02-16 16:00:00.000 20
237 00001280687 2014-02-16 16:00:00.000 2014-02-16 17:01:00.000 21
238 00001280687 2014-02-16 17:01:00.000 2014-02-16 18:00:00.000 20
236 00001280687 2014-02-17 16:00:00.000 2014-02-17 17:00:00.000 20
ClockEvent B:
EVENTID EMPID CLOCKIN CLOCKOUT PAYCODEID
228 00001280687 2014-02-16 08:00:00.000 2014-02-16 12:00:00.000 20
234 00001280687 2014-02-16 12:00:00.000 2014-02-16 13:00:00.000 21
235 00001280687 2014-02-16 13:00:00.000 2014-02-16 16:00:00.000 20
237 00001280687 2014-02-16 16:00:00.000 2014-02-16 17:01:00.000 8
238 00001280687 2014-02-16 17:01:00.000 2014-02-16 18:00:00.000 20
236 00001280687 2014-02-17 16:00:00.000 2014-02-17 17:00:00.000 20
ClockEvent C:
EVENTID EMPID CLOCKIN CLOCKOUT PAYCODEID
228 00001280687 2014-02-16 08:00:00.000 2014-02-16 12:00:00.000 20
234 00001280687 2014-02-16 12:00:00.000 2014-02-16 13:00:00.000 21
235 00001280687 2014-02-16 13:00:00.000 2014-02-16 16:00:00.000 20
237 00001280687 2014-02-16 16:00:00.000 2014-02-16 17:01:00.000 21
238 00001280687 2014-02-16 17:01:00.000 2014-02-16 18:00:00.000 20
236 00001280687 2014-02-17 16:00:00.000 2014-02-17 17:00:00.000 20
ClockEvent D:
EVENTID EMPID CLOCKIN CLOCKOUT PAYCODEID
228 00001280687 2014-02-16 08:00:00.000 2014-02-16 12:00:00.000 20
234 00001280687 2014-02-16 12:00:00.000 2014-02-16 13:00:00.000 8
235 00001280687 2014-02-16 13:00:00.000 2014-02-16 16:00:00.000 20
237 00001280687 2014-02-16 16:00:00.000 2014-02-16 17:01:00.000 8
238 00001280687 2014-02-16 17:01:00.000 2014-02-16 18:00:00.000 20
236 00001280687 2014-02-17 16:00:00.000 2014-02-17 17:00:00.000 20
ClockEvent E:
EVENTID EMPID CLOCKIN CLOCKOUT PAYCODEID
228 00001280687 2014-02-16 08:00:00.000 2014-02-16 12:00:00.000 8
234 00001280687 2014-02-16 12:00:00.000 2014-02-16 13:00:00.000 21
235 00001280687 2014-02-16 13:00:00.000 2014-02-16 16:00:00.000 8
237 00001280687 2014-02-16 16:00:00.000 2014-02-16 17:01:00.000 21
238 00001280687 2014-02-16 17:01:00.000 2014-02-16 18:00:00.000 20
236 00001280687 2014-02-17 16:00:00.000 2014-02-17 17:00:00.000 20
Ideally, a perfect query/sproc could return the following in each scenario, checking for punches 360 minutes out:
A:
EVENTID EMPID CLOCKIN CLOCKOUT PAYCODEID
234 00001280687 2014-02-16 12:00:00.000 2014-02-16 13:00:00.000 8
B:
Nada!
C:
Nada!
D:
EVENTID EMPID CLOCKIN CLOCKOUT PAYCODEID
234 00001280687 2014-02-16 12:00:00.000 2014-02-16 13:00:00.000 8
E (it could return multiples too, but I really only care about the existence of at least one meal before 6 hours):
EVENTID EMPID CLOCKIN CLOCKOUT PAYCODEID
228 00001280687 2014-02-16 08:00:00.000 2014-02-16 12:00:00.000 8
It could also do something like this, ignoring that MealRangeMax piece and just using Clockin from ClockEventShifts:
A:
MINUTE_DIFFERENCE
240
B:
MINUTE_DIFFERENCE
600
C:
MINUTE_DIFFERENCE
NULL
D:
MINUTE_DIFFERENCE
240
600 (optional)
E:
MINUTE_DIFFERENCE
0
300 (optional)
I would tend to use something like IN or BETWEEN for such things, but IN can't check the values between things though to my knowledge, and BETWEEN can only check against an X and Y value to my knowledge, but I need to continue those BETWEEN checks for the rest of the result set from ClockEventShifts. I'm still searching for some solutions, but this seems to be slightly more complex.
Does anyone have any advice or ideas in approaching this problem?
This sql statement will give you records from CLOCKEVENT where the paycode is 7 or 8 and the event occurs within ClockEventShifts clockin time and mealrangemax.
SELECT ce.*
FROM ClockEvent ce
, ClockEventShifts ces
WHERE ce.PAYCODEID IN(7, 8)
AND ce.EMPID = ces.EMPID
AND ce.CLOCKIN >= ces.CLOCKIN
AND ce.CLOCKOUT <= ces.MealRangeMax
If you only care about the ClockIn time being within the ClockEventShift range of time, you could use this:
SELECT ce.*
FROM ClockEvent ce
, ClockEventShifts ces
WHERE ce.PAYCODEID IN(7, 8)
AND ce.EMPID = ces.EMPID
AND ce.CLOCKIN BETWEEN ces.CLOCKIN AND ces.MealRangeMax