Skip to content
This repository has been archived by the owner on Oct 8, 2019. It is now read-only.

default value for 'Deleted' columns #520

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/SQLDatabase/MyDrivingDB.sql
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,11 @@ BEGIN
ALTER TABLE [dbo].[Devices] ADD DEFAULT (sysutcdatetime()) FOR [CreatedAt]
END

IF (SELECT object_definition(default_object_id) AS definition FROM sys.columns WHERE name ='Deleted' AND object_id = object_id('dbo.Devices')) IS NULL
BEGIN
ALTER TABLE [dbo].[Devices] ADD DEFAULT (0) FOR [Deleted]
END

GO
--IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[DF__IOTHubDatas__Id__36B12243]') AND type = 'D')
IF (SELECT object_definition(default_object_id) AS definition FROM sys.columns WHERE name ='Id' AND object_id = object_id('dbo.IOTHubDatas')) IS NULL
Expand All @@ -273,6 +278,11 @@ BEGIN
ALTER TABLE [dbo].[IOTHubDatas] ADD DEFAULT (sysutcdatetime()) FOR [CreatedAt]
END

IF (SELECT object_definition(default_object_id) AS definition FROM sys.columns WHERE name ='Deleted' AND object_id = object_id('dbo.IOTHubDatas')) IS NULL
BEGIN
ALTER TABLE [dbo].[IOTHubDatas] ADD DEFAULT (0) FOR [Deleted]
END

GO
--IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[DF__POIs__Id__3B75D760]') AND type = 'D')
IF (SELECT object_definition(default_object_id) AS definition FROM sys.columns WHERE name ='Id' AND object_id = object_id('dbo.POIs')) IS NULL
Expand All @@ -294,6 +304,12 @@ BEGIN
ALTER TABLE [dbo].[POIs] ADD DEFAULT ('1900-01-01T00:00:00.000') FOR [Timestamp]
END

IF (SELECT object_definition(default_object_id) AS definition FROM sys.columns WHERE name ='Deleted' AND object_id = object_id('dbo.POIs')) IS NULL
BEGIN
ALTER TABLE [dbo].[POIs] ADD DEFAULT (0) FOR [Deleted]
END


GO
--IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[DF__TripPoints__Id__403A8C7D]') AND type = 'D')
IF (SELECT object_definition(default_object_id) AS definition FROM sys.columns WHERE name ='Id' AND object_id = object_id('dbo.TripPoints')) IS NULL
Expand All @@ -308,6 +324,12 @@ BEGIN
ALTER TABLE [dbo].[TripPoints] ADD DEFAULT (sysutcdatetime()) FOR [CreatedAt]
END

IF (SELECT object_definition(default_object_id) AS definition FROM sys.columns WHERE name ='Deleted' AND object_id = object_id('dbo.TripPoints')) IS NULL
BEGIN
ALTER TABLE [dbo].[TripPoints] ADD DEFAULT (0) FOR [Deleted]
END


GO
--IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[DF__Trips__Id__44FF419A]') AND type = 'D')
IF (SELECT object_definition(default_object_id) AS definition FROM sys.columns WHERE name ='Id' AND object_id = object_id('dbo.Trips')) IS NULL
Expand All @@ -322,6 +344,11 @@ BEGIN
ALTER TABLE [dbo].[Trips] ADD DEFAULT (sysutcdatetime()) FOR [CreatedAt]
END

IF (SELECT object_definition(default_object_id) AS definition FROM sys.columns WHERE name ='Deleted' AND object_id = object_id('dbo.Trips')) IS NULL
BEGIN
ALTER TABLE [dbo].[Trips] ADD DEFAULT (0) FOR [Deleted]
END

GO
--IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[DF__UserProfiles__Id__49C3F6B7]') AND type = 'D')
IF (SELECT object_definition(default_object_id) AS definition FROM sys.columns WHERE name ='Id' AND object_id = object_id('dbo.UserProfiles')) IS NULL
Expand All @@ -336,6 +363,12 @@ BEGIN
ALTER TABLE [dbo].[UserProfiles] ADD DEFAULT (sysutcdatetime()) FOR [CreatedAt]
END

IF (SELECT object_definition(default_object_id) AS definition FROM sys.columns WHERE name ='Deleted' AND object_id = object_id('dbo.UserProfiles')) IS NULL
BEGIN
ALTER TABLE [dbo].[UserProfiles] ADD DEFAULT (0) FOR [Deleted]
END


GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_dbo.Devices_dbo.UserProfiles_UserProfile_Id]') AND parent_object_id = OBJECT_ID(N'[dbo].[Devices]'))
ALTER TABLE [dbo].[Devices] WITH CHECK ADD CONSTRAINT [FK_dbo.Devices_dbo.UserProfiles_UserProfile_Id] FOREIGN KEY([UserProfile_Id])
Expand Down
77 changes: 59 additions & 18 deletions src/StreamAnalytics/asa_hourlypbi_query.txt
Original file line number Diff line number Diff line change
@@ -1,27 +1,68 @@
WITH TripPointRaw as
(
SELECT
TripId,
TripId,
UserId,
TripDataPoint.Lat as RawLat,
TripDataPoint.Lon as RawLong,
CAST(TripDataPoint.Speed as FLOAT) as spd,
CAST(TripDataPoint.EngineRPM as FLOAT) as enginerpm,
CAST(TripDataPoint.EngineLoad as FLOAT) as engineLoad,
CAST(TripDataPoint.ShortTermFuelBank1 as FLOAT) as shortTermFuelBank,
CAST(TripDataPoint.LongTermFuelBank1 as FLOAT) as longTermFuelBank,
CAST(TripDataPoint.MAFFlowRate as FLOAT) as flowRate,
CAST(TripDataPoint.ThrottlePosition as FLOAT) as throttlePos,
CAST(TripDataPoint.Runtime as FLOAT) as runtime,
CAST(TripDataPoint.DistanceWithMIL as FLOAT) as distanceWithMIL,
CAST(TripDataPoint.RelativeThrottlePosition as FLOAT) as relativeThrottlePos,
CAST(TripDataPoint.OutsideTemperature as FLOAT) as outsideTemperature,
CAST(TripDataPoint.EngineFuelRate as FLOAT) as engineFuelRate,
CASE
WHEN CAST(TripDataPoint.Lat as FLOAT) != 255 THEN CAST(TripDataPoint.Lat as FLOAT)
ELSE NULL
END as RawLat,
CASE
WHEN CAST(TripDataPoint.Lon as FLOAT) != 255 THEN CAST(TripDataPoint.Lon as FLOAT)
ELSE NULL
END as RawLong,
CASE
WHEN CAST(TripDataPoint.Speed as FLOAT) >=0 THEN CAST(TripDataPoint.Speed as FLOAT)
ELSE NULL
END as spd,
CASE
WHEN CAST(TripDataPoint.EngineRPM as FLOAT) != -255 THEN CAST(TripDataPoint.EngineRPM as FLOAT)
ELSE NULL
END as enginerpm,
CASE
WHEN CAST(TripDataPoint.EngineLoad as FLOAT) != -255 THEN CAST(TripDataPoint.EngineLoad as FLOAT)
ELSE NULL
END as engineLoad,
CASE
WHEN CAST(TripDataPoint.ShortTermFuelBank1 as FLOAT) != -255 THEN CAST(TripDataPoint.ShortTermFuelBank1 as FLOAT)
ELSE NULL
END as shortTermFuelBank,
CASE
WHEN CAST(TripDataPoint.LongTermFuelBank1 as FLOAT) != -255 THEN CAST(TripDataPoint.LongTermFuelBank1 as FLOAT)
ELSE NULL
END as longTermFuelBank,
CASE
WHEN CAST(TripDataPoint.MAFFlowRate as FLOAT) != -255 THEN CAST(TripDataPoint.MAFFlowRate as FLOAT)
ELSE NULL
END as flowRate,
CASE
WHEN CAST(TripDataPoint.ThrottlePosition as FLOAT) != -255 THEN CAST(TripDataPoint.ThrottlePosition as FLOAT)
ELSE NULL
END as throttlePos,
CASE
WHEN CAST(TripDataPoint.Runtime as FLOAT) >=0 THEN CAST(TripDataPoint.Runtime as FLOAT)
ELSE NULL
END as runtime,
CASE
WHEN CAST(TripDataPoint.DistanceWithMIL as FLOAT) >=0 THEN CAST(TripDataPoint.DistanceWithMIL as FLOAT)
ELSE NULL
END as distanceWithMIL,
CASE
WHEN CAST(TripDataPoint.RelativeThrottlePosition as FLOAT) != -255 THEN CAST(TripDataPoint.RelativeThrottlePosition as FLOAT)
ELSE NULL
END as relativeThrottlePos,
CASE
WHEN CAST(TripDataPoint.OutsideTemperature as FLOAT) != -255 THEN CAST(TripDataPoint.OutsideTemperature as FLOAT)
ELSE NULL
END as outsideTemperature,
CASE
WHEN CAST(TripDataPoint.EngineFuelRate as FLOAT) != -255 THEN CAST(TripDataPoint.EngineFuelRate as FLOAT)
ELSE NULL
END as engineFuelRate,
TripDataPoint.VIN as vin,
TripDataPoint.RecordedTimeStamp as actualTS,
DATEADD(millisecond,- DATEPART(millisecond,TripDataPoint.RecordedTimeStamp),DATEADD(second, 5 - CAST(CEILING(DATEPART(second, TripDataPoint.RecordedTimeStamp)%5) as BIGINT),TripDataPoint.RecordedTimeStamp)) as ts,
DATEDIFF(millisecond, TripDataPoint.RecordedTimeStamp, DATEADD(millisecond,-DATEPART(millisecond,TripDataPoint.RecordedTimeStamp),DATEADD(second, 5 - CAST(CEILING(DATEPART(second, TripDataPoint.RecordedTimeStamp)%5) as BIGINT),TripDataPoint.RecordedTimeStamp))) as tsDiff,
TripDataPoint.VIN as vin,
TripDataPoint.RelativeThrottlePosition as throttle
DATEDIFF(millisecond, TripDataPoint.RecordedTimeStamp, DATEADD(millisecond,-DATEPART(millisecond,TripDataPoint.RecordedTimeStamp),DATEADD(second, 5 - CAST(CEILING(DATEPART(second, TripDataPoint.RecordedTimeStamp)%5) as BIGINT),TripDataPoint.RecordedTimeStamp))) as tsDiff
FROM
CarDeviceData TIMESTAMP by TripDataPoint.RecordedTimeStamp
WHERE
Expand Down
77 changes: 59 additions & 18 deletions src/StreamAnalytics/asa_sqlsink_powerbisink_query.txt
Original file line number Diff line number Diff line change
@@ -1,27 +1,68 @@
WITH TripPointRaw as
(
SELECT
TripId,
TripId,
UserId,
TripDataPoint.Lat as RawLat,
TripDataPoint.Lon as RawLong,
CAST(TripDataPoint.Speed as FLOAT) as spd,
CAST(TripDataPoint.EngineRPM as FLOAT) as enginerpm,
CAST(TripDataPoint.EngineLoad as FLOAT) as engineLoad,
CAST(TripDataPoint.ShortTermFuelBank1 as FLOAT) as shortTermFuelBank,
CAST(TripDataPoint.LongTermFuelBank1 as FLOAT) as longTermFuelBank,
CAST(TripDataPoint.MAFFlowRate as FLOAT) as flowRate,
CAST(TripDataPoint.ThrottlePosition as FLOAT) as throttlePos,
CAST(TripDataPoint.Runtime as FLOAT) as runtime,
CAST(TripDataPoint.DistanceWithMIL as FLOAT) as distanceWithMIL,
CAST(TripDataPoint.RelativeThrottlePosition as FLOAT) as relativeThrottlePos,
CAST(TripDataPoint.OutsideTemperature as FLOAT) as outsideTemperature,
CAST(TripDataPoint.EngineFuelRate as FLOAT) as engineFuelRate,
CASE
WHEN CAST(TripDataPoint.Lat as FLOAT) != 255 THEN CAST(TripDataPoint.Lat as FLOAT)
ELSE NULL
END as RawLat,
CASE
WHEN CAST(TripDataPoint.Lon as FLOAT) != 255 THEN CAST(TripDataPoint.Lon as FLOAT)
ELSE NULL
END as RawLong,
CASE
WHEN CAST(TripDataPoint.Speed as FLOAT) >=0 THEN CAST(TripDataPoint.Speed as FLOAT)
ELSE NULL
END as spd,
CASE
WHEN CAST(TripDataPoint.EngineRPM as FLOAT) != -255 THEN CAST(TripDataPoint.EngineRPM as FLOAT)
ELSE NULL
END as enginerpm,
CASE
WHEN CAST(TripDataPoint.EngineLoad as FLOAT) != -255 THEN CAST(TripDataPoint.EngineLoad as FLOAT)
ELSE NULL
END as engineLoad,
CASE
WHEN CAST(TripDataPoint.ShortTermFuelBank1 as FLOAT) != -255 THEN CAST(TripDataPoint.ShortTermFuelBank1 as FLOAT)
ELSE NULL
END as shortTermFuelBank,
CASE
WHEN CAST(TripDataPoint.LongTermFuelBank1 as FLOAT) != -255 THEN CAST(TripDataPoint.LongTermFuelBank1 as FLOAT)
ELSE NULL
END as longTermFuelBank,
CASE
WHEN CAST(TripDataPoint.MAFFlowRate as FLOAT) != -255 THEN CAST(TripDataPoint.MAFFlowRate as FLOAT)
ELSE NULL
END as flowRate,
CASE
WHEN CAST(TripDataPoint.ThrottlePosition as FLOAT) != -255 THEN CAST(TripDataPoint.ThrottlePosition as FLOAT)
ELSE NULL
END as throttlePos,
CASE
WHEN CAST(TripDataPoint.Runtime as FLOAT) >=0 THEN CAST(TripDataPoint.Runtime as FLOAT)
ELSE NULL
END as runtime,
CASE
WHEN CAST(TripDataPoint.DistanceWithMIL as FLOAT) >=0 THEN CAST(TripDataPoint.DistanceWithMIL as FLOAT)
ELSE NULL
END as distanceWithMIL,
CASE
WHEN CAST(TripDataPoint.RelativeThrottlePosition as FLOAT) != -255 THEN CAST(TripDataPoint.RelativeThrottlePosition as FLOAT)
ELSE NULL
END as relativeThrottlePos,
CASE
WHEN CAST(TripDataPoint.OutsideTemperature as FLOAT) != -255 THEN CAST(TripDataPoint.OutsideTemperature as FLOAT)
ELSE NULL
END as outsideTemperature,
CASE
WHEN CAST(TripDataPoint.EngineFuelRate as FLOAT) != -255 THEN CAST(TripDataPoint.EngineFuelRate as FLOAT)
ELSE NULL
END as engineFuelRate,
TripDataPoint.VIN as vin,
TripDataPoint.RecordedTimeStamp as actualTS,
DATEADD(millisecond,- DATEPART(millisecond,TripDataPoint.RecordedTimeStamp),DATEADD(second, 5 - CAST(CEILING(DATEPART(second, TripDataPoint.RecordedTimeStamp)%5) as BIGINT),TripDataPoint.RecordedTimeStamp)) as ts,
DATEDIFF(millisecond, TripDataPoint.RecordedTimeStamp, DATEADD(millisecond,-DATEPART(millisecond,TripDataPoint.RecordedTimeStamp),DATEADD(second, 5 - CAST(CEILING(DATEPART(second, TripDataPoint.RecordedTimeStamp)%5) as BIGINT),TripDataPoint.RecordedTimeStamp))) as tsDiff,
TripDataPoint.VIN as vin,
TripDataPoint.RelativeThrottlePosition as throttle
DATEDIFF(millisecond, TripDataPoint.RecordedTimeStamp, DATEADD(millisecond,-DATEPART(millisecond,TripDataPoint.RecordedTimeStamp),DATEADD(second, 5 - CAST(CEILING(DATEPART(second, TripDataPoint.RecordedTimeStamp)%5) as BIGINT),TripDataPoint.RecordedTimeStamp))) as tsDiff
FROM
CarDeviceData TIMESTAMP by TripDataPoint.RecordedTimeStamp
WHERE
Expand Down