-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path07_Phase3_Refinement_And_Data.sql
More file actions
45 lines (37 loc) · 1.22 KB
/
07_Phase3_Refinement_And_Data.sql
File metadata and controls
45 lines (37 loc) · 1.22 KB
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
USE SmartHomeDB;
GO
ALTER TABLE Devices
ADD DeviceCode AS (UPPER(SUBSTRING(DeviceName, 1, 3)) + '-' + CAST(RoomID AS VARCHAR) + '-' + CAST(DeviceID AS VARCHAR));
GO
CREATE NONCLUSTERED INDEX IX_SensorReadings_DeviceID
ON SensorReadings(DeviceID);
GO
CREATE NONCLUSTERED INDEX IX_Devices_RoomID
ON Devices(RoomID);
GO
DECLARE @i INT = 1;
DECLARE @RandDeviceID INT;
DECLARE @RandValue FLOAT;
DECLARE @RandTemp FLOAT;
WHILE @i <= 100
BEGIN
SET @RandDeviceID = FLOOR(RAND() * 5) + 1;
SET @RandTemp = CAST(18 + (RAND() * 15) AS DECIMAL(4,1));
IF @RandDeviceID = 2
BEGIN
INSERT INTO SensorReadings (DeviceID, ReadingValue, Unit, RecordedAt)
VALUES (@RandDeviceID, @RandTemp, 'Celsius', DATEADD(MINUTE, -@i, GETDATE()));
END
ELSE IF @RandDeviceID = 4
BEGIN
INSERT INTO SensorReadings (DeviceID, ReadingValue, Unit, RecordedAt)
VALUES (@RandDeviceID, FLOOR(RAND() * 2), 'Boolean', DATEADD(MINUTE, -@i, GETDATE()));
END
ELSE
BEGIN
INSERT INTO SensorReadings (DeviceID, ReadingValue, Unit, RecordedAt)
VALUES (@RandDeviceID, 0, 'Generic', DATEADD(MINUTE, -@i, GETDATE()));
END
SET @i = @i + 1;
END;
GO