declare @EntityId bigint = 4837 
declare @VendorId bigint = 11  -- Flanagan Kitchner 161
 
  select distinct
      ez.EntityID as EntityId,
      vizr.VendorCode As VendorCode,
	  'ZoneRule' As RuleTypes,
	  vi.VendorId,
      vi.ItemID,
	  vizr.RuleType, 
	  vi.VendorDescription,
	  vizr.UpdatedBy, 
	  vizr.ApplyDate,
	  vizr.ExpirationDate
  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
  where 
	  ez.EntityID = @EntityId 
	  and vizr.Deleted = 0
	  and vi.VendorID = @VendorId
	  and (vizr.ExpirationDate > getdate() or vizr.ExpirationDate is NULL)
  UNION ALL 
  select distinct
      vier.EntityId as EntityId,
      vier.VendorCode,
	  'EntityRule' As RuleTypes,
	  vi.VendorID,
	  vi.ItemID,
	  vi.VendorDescription,
	  vier.RuleType, 
	  vier.UpdatedBy, 
	  vier.ApplyDate,
	  vier.ExpirationDate
  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
  where 
	  vier.EntityID = @EntityId 
	  and vier.Deleted = 0
	  	  and (vier.ExpirationDate > getdate() or vier.ExpirationDate is NULL)
  order by VendorDescription, RuleTypes,  VendorID, ItemID 




