declare @timetill datetime =  DATEADD(MINUTE, 10, GETDATE())

insert into ActvtAlrtStsTbl 
	( [iGrpId], [iAlertTypeId], [iAlertStstus], [dStatusDateTime], [sAlrt], [iCpId], [sPrm1], [sPrm2], [sPrm3], [iAlrtHndlrId], [iSubStsGrpId], [sAlertInfo], [AlertCloseTime], 
	[AlertCloseCode], [RuleId], [EventCode], [EAlertTypeId], [ServerUtcTime], [PanelTime], [PanelUtcTime], [overrideClosingUser], [BaseDeviceType], [CreatedUtc], [RawId])
select	68,	8,	3,	dateadd(second,1,max([dStatusDateTime])),'USER_COMMAND',alrt.icpid,	NULL,NULL,	27,[iAlrtHndlrId],[iSubStsGrpId],	'',	getutcdate(),
	NULL,0,NULL,NULL,GetUtcdate(),	dateadd(second,1,max([PanelTime])), dateadd(second,1,max([PanelUtcTime])),'',NULL,null,	'00000000-0000-0000-0000-000000000000'
FROM
	[ActvtAlrtStsTbl] alrt
	JOIN CpTbl AS ct ON ct.CpId = alrt.iCpId
	JOIN Accounts a ON a.AccountNumber = ct.Account
	JOIN ServicePackages sp ON sp.Id = a.ServicePackageId
	WHERE 
		sp.CodeName in ('PERS-E','Family')
		AND alrt.PanelTime <@timetill
		AND alrt.iAlertStstus < 3
group by icpid,iAlrtHndlrId,iSubStsGrpId


--For PERS and Family
	UPDATE 
		alrt
	SET iAlertStstus = 3

	FROM
	[ActvtAlrtStsTbl] alrt
	JOIN CpTbl AS ct ON ct.CpId = alrt.iCpId
	JOIN Accounts a ON a.AccountNumber = ct.Account
	JOIN ServicePackages sp ON sp.Id = a.ServicePackageId
	WHERE 
		sp.CodeName in ('PERS-E','Family')
		AND alrt.PanelTime < @timetill
		AND alrt.iAlertStstus < 3

--For PRO just technical events
	UPDATE 
		alrt
	SET iAlertStstus = 3
	FROM
	[ActvtAlrtStsTbl] alrt
	JOIN CpTbl AS ct ON ct.CpId = alrt.iCpId
	JOIN Accounts a ON a.AccountNumber = ct.Account
	JOIN ServicePackages sp ON sp.Id = a.ServicePackageId
	WHERE 
		sp.CodeName in ('Pro')
		AND alrt.PanelTime < @timetill
		AND alrt.iAlertTypeId = 4
		AND alrt.iAlertStstus < 3

