forked from niallermoran/SmartHVACDemo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SA1.sql
52 lines (40 loc) · 1.48 KB
/
SA1.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
/* we don't really want to push the entire stream out, this is just run for a while to get some Date for training our predictive model */
select *
INTO
Streaming
from IoTHub TIMESTAMP by Time
/* The tumbling window below will take an average every 15 minutes and output one record every 15 minutes to the Table 'Tumbling' */
SELECT
DeviceId,
DeviceName,
FloorArea,
Avg(ExternalTemp) as ExternalTemp,
Max(Time) as Time,
Avg(Internaltemp) as InternalTemp,
sum(IsHeatingOn) as IsHeatingOnTotal,
count(*) as TotalReadings,
sum(IsHeatingOn)*100/count(*) as HeatOnPercentage
Into Tumbling
FROM
IoTHub TIMESTAMP BY Time
GROUP BY
TumblingWindow(minute, 15), DeviceId, DeviceName, FloorArea
/* The sliding window below will output to the Table 'sliding' for every data point with averages for 15 minutes before, this is a rolling average every second.
We use this sliding window to check for alert conditions, e.g. average temperature over any 15 minutes should not go below 18 or over 23
*/
SELECT
DeviceId,
DeviceName,
FloorArea,
Avg(ExternalTemp) as ExternalTemp,
Max(Time) as Time,
Avg(Internaltemp) as InternalTemp,
sum(IsHeatingOn) as IsHeatingOnTotal,
count(*) as TotalReadings,
sum(IsHeatingOn)*100/count(*) as HeatOnPercentage
Into Sliding
FROM
IoTHub TIMESTAMP BY Time
GROUP BY
SlidingWindow(minute, 15), DeviceId, DeviceName, FloorArea
/* Having Avg(InternalTemp) <= 18 or avg(InternalTemp) >= 23*/