Table of Contents [Hide/Show]
Overview First Generation Solution Approach Table Definitions UserProfile Table AppContact Table AppContactRule Table SQL Code Second Generation Solution Approach The UserEmailSetting Table SQL Code
create view dbo.AppContactRuleExtended as with MyData as ( select AppId = coalesce(AppId, -1) ,EmailFlag ,RequestTypeId ,UserTypeId from AppContactRule ) select * ,MaxAppId = max(AppId) over (partition by RequestTypeId, UserTypeId) from MyData go select SenderEmail = (select Email from dbo.UserProfile where LanId = @CreatedBy) ,RecipientEmail = u.Email from dbo.AppContactRuleExtended m inner join dbo.AppContact ac on ac.UserTypeId = m.UserTypeId -- and ac.AppId = m.AppId (ac.AppId might be -1) inner join dbo.UserProfile u on ac.UserProfileId = u.UserProfileId where 1=1 and ac.AppId = @AppId and m.AppId = m.MaxAppId and m.EmailFlag = 1 and m.AppId in (-1, @AppId) and m.RequestTypeId = @RequestTypeId
;with ExclusionsPrep as ( select m.UserProfileId ,m.AppId ,m.EmailFlag ,u.Email ,MaxAppId = max(m.AppId) over (partition by m.UserProfileId) from dbo.UserEmailSettingExtended m inner join dbo.UserProfile u on m.UserProfileId = u.UserProfileId where 1=1 and m.AppId in (-1, m.MaxAppId) --and m.EmailFlag = 0 and m.AppId in (-1, @AppId) and m.RequestTypeId = @RequestTypeId ) ,Exclusions as ( select * from ExclusionsPrep ep where ep.AppId = ep.MaxAppId and ep.EmailFlag = 0 ) select SenderEmail = (select Email from dbo.EmployeeActive where LanId = @CreatedBy) ,RecipientEmail = u.Email from dbo.AppContactRuleExtended m inner join dbo.AppContact ac on ac.UserTypeId = m.UserTypeId -- and ac.AppId = m.AppId (ac.AppId might be -1) inner join dbo.UserProfile u on ac.UserProfileId = u.UserProfileId where 1=1 and ac.AppId = @AppId and m.AppId = m.MaxAppId and m.EmailFlag = 1 and m.AppId in (-1, @AppId) and m.RequestTypeId = @RequestTypeId and u.UserProfileId not in (select UserProfileId from Exclusions)
ScrewTurn Wiki version 3.0.1.400. Some of the icons created by FamFamFam. Except where noted, all contents Copyright © 1999-2024, Patrick Jasinski.