--notice this script should not run in 216 , there is a patch to remove the empty severities (with min/max null)

DECLARE @TeleHealthVendorSeverityThresholds TABLE (
	VendorId int NOT NULL,
	SeverityLevel int NOT NULL,
	FieldCode nvarchar(500) NOT NULL,
	MeasurmentUnit nvarchar(500) NOT NULL,
	MinThreshold float NULL,
	MaxThreshold float NULL,
	ContextValue nvarchar(50) null,
	ConditionTypeId int null
);

INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold)  select vendorid,-3,'OxygenSaturation','Percent',null,91 from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold)  select vendorid,-2,'OxygenSaturation','Percent',92,93 from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold)  select vendorid,-1,'OxygenSaturation','Percent',94,95 from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold)  select vendorid,0, 'OxygenSaturation','Percent',96,null from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold)  select vendorid,1, 'OxygenSaturation','Percent',null,null from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold)  select vendorid,2, 'OxygenSaturation','Percent',null,null from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold)  select vendorid,3, 'OxygenSaturation','Percent',null,null from vendortbl 

INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold)  select vendorid,-3,'BodyTemperature','Fahrenheit',null,95 from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold)  select vendorid,-2,'BodyTemperature','Fahrenheit',null,null from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold)  select vendorid,-1,'BodyTemperature','Fahrenheit',95.1,96.9 from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold)  select vendorid,0, 'BodyTemperature','Fahrenheit',97,100.4 from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold)  select vendorid,1, 'BodyTemperature','Fahrenheit',100.5,102.2 from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold)  select vendorid,2, 'BodyTemperature','Fahrenheit',102.3,null from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold)  select vendorid,3, 'BodyTemperature','Fahrenheit',null,null from vendortbl 

INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold)  select vendorid,-3,'BodyTemperature','Celsius',null,35 from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold)  select vendorid,-2,'BodyTemperature','Celsius',null,null from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold)  select vendorid,-1,'BodyTemperature','Celsius',35.1,36 from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold)  select vendorid,0, 'BodyTemperature','Celsius',36.1,38 from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold)  select vendorid,1, 'BodyTemperature','Celsius',38.1,39 from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold)  select vendorid,2, 'BodyTemperature','Celsius',39.1,null from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold)  select vendorid,3, 'BodyTemperature','Celsius',null,null from vendortbl 

INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold)  select vendorid,-3,'SystolicBloodPressure','MmHg',null,90 from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold)  select vendorid,-2,'SystolicBloodPressure','MmHg',91,100 from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold)  select vendorid,-1,'SystolicBloodPressure','MmHg',101,110 from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold)  select vendorid,0, 'SystolicBloodPressure','MmHg',111,120 from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold)  select vendorid,1, 'SystolicBloodPressure','MmHg',121,140 from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold)  select vendorid,2, 'SystolicBloodPressure','MmHg',141,180 from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold)  select vendorid,3, 'SystolicBloodPressure','MmHg',181,null from vendortbl 

INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold)  select vendorid,-3,'DiastolicBloodPressure','MmHg',null,null from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold)  select vendorid,-2,'DiastolicBloodPressure','MmHg',null,null from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold)  select vendorid,-1,'DiastolicBloodPressure','MmHg',null,null from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold)  select vendorid,0, 'DiastolicBloodPressure','MmHg',null,80 from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold)  select vendorid,1, 'DiastolicBloodPressure','MmHg',81,90 from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold)  select vendorid,2, 'DiastolicBloodPressure','MmHg',91,100 from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold)  select vendorid,3, 'DiastolicBloodPressure','MmHg',101,null from vendortbl 

INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold)  select vendorid,-3,'SystolicBloodPressure','kPa',null,12 from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold)  select vendorid,-2,'SystolicBloodPressure','kPa',12.1,13.4 from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold)  select vendorid,-1,'SystolicBloodPressure','kPa',13.5,14.7 from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold)  select vendorid,0, 'SystolicBloodPressure','kPa',14.8,16 from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold)  select vendorid,1, 'SystolicBloodPressure','kPa',16.1,18.7 from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold)  select vendorid,2, 'SystolicBloodPressure','kPa',18.8,24 from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold)  select vendorid,3, 'SystolicBloodPressure','kPa',24.1,null from vendortbl 

INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold)  select vendorid,-3,'DiastolicBloodPressure','kPa',null,null from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold)  select vendorid,-2,'DiastolicBloodPressure','kPa',null,null from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold)  select vendorid,-1,'DiastolicBloodPressure','kPa',null,null from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold)  select vendorid,0, 'DiastolicBloodPressure','kPa',null,10.6 from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold)  select vendorid,1, 'DiastolicBloodPressure','kPa',10.7,12 from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold)  select vendorid,2, 'DiastolicBloodPressure','kPa',12.1,13.3 from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold)  select vendorid,3, 'DiastolicBloodPressure','kPa',13.4,null from vendortbl 


INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold)  select vendorid,-3,'PulseRate','Bpm',null,40 from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold)  select vendorid,-2,'PulseRate','Bpm',null,null from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold)  select vendorid,-1,'PulseRate','Bpm',41,50 from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold)  select vendorid,0, 'PulseRate','Bpm',51,90 from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold)  select vendorid,1, 'PulseRate','Bpm',91,110 from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold)  select vendorid,2, 'PulseRate','Bpm',111,130 from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold)  select vendorid,3, 'PulseRate','Bpm',131,null from vendortbl 

-- non diabetic - Before meal
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold,ContextValue,ConditionTypeId)  select vendorid,-3 ,'GlucoseLevel','MgDL',null	,69		,'BeforeMeal'	,null	from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold,ContextValue,ConditionTypeId)  select vendorid,-2 ,'GlucoseLevel','MgDL',null	,null	,'BeforeMeal'	,null	from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold,ContextValue,ConditionTypeId)  select vendorid,-1 ,'GlucoseLevel','MgDL',null	,null	,'BeforeMeal'	,null	from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold,ContextValue,ConditionTypeId)  select vendorid, 0 ,'GlucoseLevel','MgDL',70	,100	,'BeforeMeal'	,null	from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold,ContextValue,ConditionTypeId)  select vendorid, 1 ,'GlucoseLevel','MgDL',null	,null	,'BeforeMeal'	,null	from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold,ContextValue,ConditionTypeId)  select vendorid, 2 ,'GlucoseLevel','MgDL',null	,null	,'BeforeMeal'	,null	from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold,ContextValue,ConditionTypeId)  select vendorid, 3 ,'GlucoseLevel','MgDL',101	,null	,'BeforeMeal'	,null	from vendortbl 
-- non diabetic - after meal
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold,ContextValue,ConditionTypeId)  select vendorid,-3,'GlucoseLevel','MgDL',null	,69		,'AfterMeal'	,null	from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold,ContextValue,ConditionTypeId)  select vendorid,-2,'GlucoseLevel','MgDL',null	,null	,'AfterMeal'	,null	from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold,ContextValue,ConditionTypeId)  select vendorid,-1,'GlucoseLevel','MgDL',null	,null	,'AfterMeal'	,null	from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold,ContextValue,ConditionTypeId)  select vendorid, 0,'GlucoseLevel','MgDL',70	,140	,'AfterMeal'	,null	from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold,ContextValue,ConditionTypeId)  select vendorid, 1,'GlucoseLevel','MgDL',null	,null	,'AfterMeal'	,null	from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold,ContextValue,ConditionTypeId)  select vendorid, 2,'GlucoseLevel','MgDL',null	,null	,'AfterMeal'	,null	from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold,ContextValue,ConditionTypeId)  select vendorid, 3,'GlucoseLevel','MgDL',141	,null	,'AfterMeal'	,null	from vendortbl 

-- type 2 diabetic - Before meal
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold,ContextValue,ConditionTypeId)  select vendorid,-3 ,'GlucoseLevel','MgDL',null	,69		,'BeforeMeal'	,4	from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold,ContextValue,ConditionTypeId)  select vendorid,-2 ,'GlucoseLevel','MgDL',null	,null	,'BeforeMeal'	,4	from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold,ContextValue,ConditionTypeId)  select vendorid,-1 ,'GlucoseLevel','MgDL',null	,null	,'BeforeMeal'	,4	from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold,ContextValue,ConditionTypeId)  select vendorid, 0 ,'GlucoseLevel','MgDL',70	,130	,'BeforeMeal'	,4	from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold,ContextValue,ConditionTypeId)  select vendorid, 1 ,'GlucoseLevel','MgDL',null	,null	,'BeforeMeal'	,4	from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold,ContextValue,ConditionTypeId)  select vendorid, 2 ,'GlucoseLevel','MgDL',null	,null	,'BeforeMeal'	,4	from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold,ContextValue,ConditionTypeId)  select vendorid, 3 ,'GlucoseLevel','MgDL',131	,null	,'BeforeMeal'	,4	from vendortbl 
-- type 2 diabetic - after meal
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold,ContextValue,ConditionTypeId)  select vendorid,-3,'GlucoseLevel','MgDL',null	,69		,'AfterMeal'	,4	from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold,ContextValue,ConditionTypeId)  select vendorid,-2,'GlucoseLevel','MgDL',null	,null	,'AfterMeal'	,4	from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold,ContextValue,ConditionTypeId)  select vendorid,-1,'GlucoseLevel','MgDL',null	,null	,'AfterMeal'	,4	from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold,ContextValue,ConditionTypeId)  select vendorid,0 ,'GlucoseLevel','MgDL',70	,150	,'AfterMeal'	,4	from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold,ContextValue,ConditionTypeId)  select vendorid,1 ,'GlucoseLevel','MgDL',null	,null	,'AfterMeal'	,4	from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold,ContextValue,ConditionTypeId)  select vendorid,2 ,'GlucoseLevel','MgDL',null	,null	,'AfterMeal'	,4	from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold,ContextValue,ConditionTypeId)  select vendorid,3 ,'GlucoseLevel','MgDL',151	,null	,'AfterMeal'	,4	from vendortbl 

-- type 1 diabetic - Before meal
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold,ContextValue,ConditionTypeId)  select vendorid,-3,'GlucoseLevel','MgDL',null	,69		,'BeforeMeal'	,3	from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold,ContextValue,ConditionTypeId)  select vendorid,-2,'GlucoseLevel','MgDL',null	,null	,'BeforeMeal'	,3	from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold,ContextValue,ConditionTypeId)  select vendorid,-1,'GlucoseLevel','MgDL',null	,null	,'BeforeMeal'	,3	from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold,ContextValue,ConditionTypeId)  select vendorid,0 ,'GlucoseLevel','MgDL',70	,130	,'BeforeMeal'	,3	from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold,ContextValue,ConditionTypeId)  select vendorid,1 ,'GlucoseLevel','MgDL',null	,null	,'BeforeMeal'	,3	from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold,ContextValue,ConditionTypeId)  select vendorid,2 ,'GlucoseLevel','MgDL',null	,null	,'BeforeMeal'	,3	from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold,ContextValue,ConditionTypeId)  select vendorid,3 ,'GlucoseLevel','MgDL',131	,null	,'BeforeMeal'	,3	from vendortbl 
-- type 1 diabetic - after meal																																																	 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold,ContextValue,ConditionTypeId)  select vendorid,-3,'GlucoseLevel','MgDL',null	,89		,'AfterMeal'	,3	from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold,ContextValue,ConditionTypeId)  select vendorid,-2,'GlucoseLevel','MgDL',null	,null	,'AfterMeal'	,3	from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold,ContextValue,ConditionTypeId)  select vendorid,-1,'GlucoseLevel','MgDL',null	,null	,'AfterMeal'	,3	from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold,ContextValue,ConditionTypeId)  select vendorid,0 ,'GlucoseLevel','MgDL',90	,160	,'AfterMeal'	,3	from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold,ContextValue,ConditionTypeId)  select vendorid,1 ,'GlucoseLevel','MgDL',null	,null	,'AfterMeal'	,3	from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold,ContextValue,ConditionTypeId)  select vendorid,2 ,'GlucoseLevel','MgDL',null	,null	,'AfterMeal'	,3	from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold,ContextValue,ConditionTypeId)  select vendorid,3 ,'GlucoseLevel','MgDL',161	,null	,'AfterMeal'	,3	from vendortbl 


--
-- non diabetic - Before meal
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold,ContextValue,ConditionTypeId)  select vendorid,-3 ,'GlucoseLevel','MmolL',null	,1243		,'BeforeMeal'	,null	from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold,ContextValue,ConditionTypeId)  select vendorid,-2 ,'GlucoseLevel','MmolL',null	,null	,'BeforeMeal'	,null	from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold,ContextValue,ConditionTypeId)  select vendorid,-1 ,'GlucoseLevel','MmolL',null	,null	,'BeforeMeal'	,null	from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold,ContextValue,ConditionTypeId)  select vendorid, 0 ,'GlucoseLevel','MmolL',1244	,2702	,'BeforeMeal'	,null	from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold,ContextValue,ConditionTypeId)  select vendorid, 1 ,'GlucoseLevel','MmolL',null	,null	,'BeforeMeal'	,null	from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold,ContextValue,ConditionTypeId)  select vendorid, 2 ,'GlucoseLevel','MmolL',null	,null	,'BeforeMeal'	,null	from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold,ContextValue,ConditionTypeId)  select vendorid, 3 ,'GlucoseLevel','MmolL',2703	,null	,'BeforeMeal'	,null	from vendortbl 
-- non diabetic - after meal
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold,ContextValue,ConditionTypeId)  select vendorid,-3,'GlucoseLevel','MmolL',null	,1243		,'AfterMeal'	,null	from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold,ContextValue,ConditionTypeId)  select vendorid,-2,'GlucoseLevel','MmolL',null	,null	,'AfterMeal'	,null	from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold,ContextValue,ConditionTypeId)  select vendorid,-1,'GlucoseLevel','MmolL',null	,null	,'AfterMeal'	,null	from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold,ContextValue,ConditionTypeId)  select vendorid, 0,'GlucoseLevel','MmolL',1244	,2522	,'AfterMeal'	,null	from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold,ContextValue,ConditionTypeId)  select vendorid, 1,'GlucoseLevel','MmolL',null	,null	,'AfterMeal'	,null	from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold,ContextValue,ConditionTypeId)  select vendorid, 2,'GlucoseLevel','MmolL',null	,null	,'AfterMeal'	,null	from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold,ContextValue,ConditionTypeId)  select vendorid, 3,'GlucoseLevel','MmolL',2523	,null	,'AfterMeal'	,null	from vendortbl 

-- type 2 diabetic - Before meal
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold,ContextValue,ConditionTypeId)  select vendorid,-3 ,'GlucoseLevel','MmolL',null	,1243		,'BeforeMeal'	,4	from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold,ContextValue,ConditionTypeId)  select vendorid,-2 ,'GlucoseLevel','MmolL',null	,null	,'BeforeMeal'	,4	from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold,ContextValue,ConditionTypeId)  select vendorid,-1 ,'GlucoseLevel','MmolL',null	,null	,'BeforeMeal'	,4	from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold,ContextValue,ConditionTypeId)  select vendorid, 0 ,'GlucoseLevel','MmolL',1244	,2342	,'BeforeMeal'	,4	from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold,ContextValue,ConditionTypeId)  select vendorid, 1 ,'GlucoseLevel','MmolL',null	,null	,'BeforeMeal'	,4	from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold,ContextValue,ConditionTypeId)  select vendorid, 2 ,'GlucoseLevel','MmolL',null	,null	,'BeforeMeal'	,4	from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold,ContextValue,ConditionTypeId)  select vendorid, 3 ,'GlucoseLevel','MmolL',2343	,null	,'BeforeMeal'	,4	from vendortbl 
-- type 2 diabetic - after meal
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold,ContextValue,ConditionTypeId)  select vendorid,-3,'GlucoseLevel','MmolL',null	,1243		,'AfterMeal'	,4	from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold,ContextValue,ConditionTypeId)  select vendorid,-2,'GlucoseLevel','MmolL',null	,null	,'AfterMeal'	,4	from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold,ContextValue,ConditionTypeId)  select vendorid,-1,'GlucoseLevel','MmolL',null	,null	,'AfterMeal'	,4	from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold,ContextValue,ConditionTypeId)  select vendorid,0 ,'GlucoseLevel','MmolL',1244	,2702	,'AfterMeal'	,4	from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold,ContextValue,ConditionTypeId)  select vendorid,1 ,'GlucoseLevel','MmolL',null	,null	,'AfterMeal'	,4	from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold,ContextValue,ConditionTypeId)  select vendorid,2 ,'GlucoseLevel','MmolL',null	,null	,'AfterMeal'	,4	from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold,ContextValue,ConditionTypeId)  select vendorid,3 ,'GlucoseLevel','MmolL',2703	,null	,'AfterMeal'	,4	from vendortbl 

-- type 1 diabetic - Before meal
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold,ContextValue,ConditionTypeId)  select vendorid,-3,'GlucoseLevel','MmolL',null	,1243		,'BeforeMeal'	,3	from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold,ContextValue,ConditionTypeId)  select vendorid,-2,'GlucoseLevel','MmolL',null	,null	,'BeforeMeal'	,3	from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold,ContextValue,ConditionTypeId)  select vendorid,-1,'GlucoseLevel','MmolL',null	,null	,'BeforeMeal'	,3	from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold,ContextValue,ConditionTypeId)  select vendorid,0 ,'GlucoseLevel','MmolL',1244	,2342	,'BeforeMeal'	,3	from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold,ContextValue,ConditionTypeId)  select vendorid,1 ,'GlucoseLevel','MmolL',null	,null	,'BeforeMeal'	,3	from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold,ContextValue,ConditionTypeId)  select vendorid,2 ,'GlucoseLevel','MmolL',null	,null	,'BeforeMeal'	,3	from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold,ContextValue,ConditionTypeId)  select vendorid,3 ,'GlucoseLevel','MmolL',2343	,null	,'BeforeMeal'	,3	from vendortbl 
-- type 1 diabetic - after meal																																																	 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold,ContextValue,ConditionTypeId)  select vendorid,-3,'GlucoseLevel','MmolL',null	,1603		,'AfterMeal'	,3	from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold,ContextValue,ConditionTypeId)  select vendorid,-2,'GlucoseLevel','MmolL',null	,null	,'AfterMeal'	,3	from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold,ContextValue,ConditionTypeId)  select vendorid,-1,'GlucoseLevel','MmolL',null	,null	,'AfterMeal'	,3	from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold,ContextValue,ConditionTypeId)  select vendorid,0 ,'GlucoseLevel','MmolL',1604	,2882	,'AfterMeal'	,3	from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold,ContextValue,ConditionTypeId)  select vendorid,1 ,'GlucoseLevel','MmolL',null	,null	,'AfterMeal'	,3	from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold,ContextValue,ConditionTypeId)  select vendorid,2 ,'GlucoseLevel','MmolL',null	,null	,'AfterMeal'	,3	from vendortbl 
INSERT INTO @TeleHealthVendorSeverityThresholds(VendorId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold,ContextValue,ConditionTypeId)  select vendorid,3 ,'GlucoseLevel','MmolL',2883	,null	,'AfterMeal'	,3	from vendortbl 


truncate table TeleHealthVendorSeverityThresholds


	MERGE INTO dbo.TeleHealthVendorSeverityThresholds  AS TARGET  
		USING @TeleHealthVendorSeverityThresholds AS SOURCE  
		ON   target.VendorId=source.VendorId and
			 target.SeverityLevel =source.SeverityLevel and
			 target.FieldCode =source.FieldCode and
			 target.MeasurmentUnit =source.MeasurmentUnit and
			 (target.ContextValue=source.ContextValue or (target.ContextValue is null and source.ContextValue is null ))and
			 (target.ConditionTypeId= source.ConditionTypeId or (target.ConditionTypeId is null and source.ConditionTypeId is null))
		WHEN NOT MATCHED BY TARGET THEN   
			INSERT (VendorId, SeverityLevel, FieldCode, MeasurmentUnit, MinThreshold, MaxThreshold,contextValue,conditionTypeId) 
			VALUES (source.VendorId, source.SeverityLevel, source.FieldCode, source.MeasurmentUnit, source.MinThreshold, source.MaxThreshold, source.ContextValue, source.ConditionTypeId)  
	OUTPUT $action, SOURCE.*;


truncate table TeleHealthResidentSeverityThresholds

INSERT INTO dbo.TeleHealthResidentSeverityThresholds(ResidentId,SeverityLevel,FieldCode,MeasurmentUnit,MinThreshold,MaxThreshold,ContextValue,ConditionTypeId)
											select u.cmsuserid , SeverityLevel, FieldCode, MeasurmentUnit, MinThreshold, MaxThreshold,v.ContextValue,v.ConditionTypeId
from UserSettings u 
	join CmsUserTbl c on c.cmsuserid=u.CmsUserId
	join TeleHealthVendorSeverityThresholds v on v.VendorId=c.VendorId
where  u.SettingTypeId=1 and u.value='True' 
	and 0 = (select count(1) 
			 from TeleHealthResidentSeverityThresholds r 
			 where  r.ResidentId=c.CmsUserId and
					r.SeverityLevel =v.SeverityLevel and
					r.FieldCode =v.FieldCode and
					r.MeasurmentUnit =v.MeasurmentUnit and
					(r.ContextValue=v.ContextValue or (r.ContextValue is null and v.ContextValue is null )) and
					(r.ConditionTypeId= v.ConditionTypeId or (r.ConditionTypeId is null and v.ConditionTypeId is null))
					)
order by u.CmsUserId,v.FieldCode,v.SeverityLevel
