declare @EntityId bigint = 4808
declare @VendorId bigint = 152

--select * from tbvendor where vendorname like '%home%'

select distinct
    ez.EntityID as EntityId,
    vizr.VendorCode As VendorCode,
     'ZoneRule' As RuleTypes, 
     vizr.ZoneId,  
    vi.ItemID,
    vi.VendorID,	
     vi.VendorDescription,
     vizr.RuleType,
     vizr.UpdatedBy,
     vizr.ApplyDate
     
from tbVendorItemZoneRule vizr with (nolock)
     inner join tbEntityZone ez with (nolock) on vizr.ZoneId= ez.ZoneID
     inner join tbVendorItem vi with (nolock) on vi.VendorCode = vizr.VendorCode
    --inner join tbVendorEntityItem vei with (nolock) on vi.VendorID = vei.VendorID and vei.EntityID = @EntityId and vi.ItemID = vei.ItemID  --comment this line to show items before assignment
where
     ez.EntityID = @EntityId
     and vizr.Deleted = 0
     and vi.VendorID = @VendorId

UNION ALL

select 
    vier.EntityId as EntityId,
    vier.VendorCode,
     'EntityRule' As RuleTypes,
      null,       
     vi.ItemID,
      vi.VendorID,
     vi.VendorDescription,
     vier.RuleType,
     vier.UpdatedBy,
     vier.ApplyDate
from tbVendorItemEntityRule vier with (nolock)
    inner join tbVendorItem vi with (nolock) on vi.VendorCode = vier.VendorCode
    inner join tbVendorEntityItem vei with (nolock) on vi.VendorID = vei.VendorID and vei.EntityID = @EntityId and vi.ItemID = vei.ItemID --comment this line to show items before assignment
where
     vier.EntityID = @EntityId
     and vier.Deleted = 0
     and vi.VendorID = @VendorId
order by VendorCode, RuleTypes,  VendorID, ItemID


