
Declare @VendorCode varchar(50) = '101051'
Declare @Entityid bigint = 902
Declare @VendorId bigint = 10  -- 10 MBM

select	top 1 vei.VendorCode,
		i.PerformStockTake,
		tsc.StockCountDate,
		ei.AutoStockCount,
		i.Description,
		vei.vendordescription,
		iq.fOnHand_qty,
		ei.InventoryUnitCost,
		vei.DiscontinueDateOverrideEnabled,
		i.itemcode,
		i.itemid,
		vei.purchaseunit	
		--vei.purchasePrice,
		--*
from	tbVendorEntityItem vei  with (nolock)
join	tbItem i  with (nolock)
	on vei.itemid = i.itemid
join	tbTransactionStockCount tsc
	on i.itemid = tsc.itemid and vei.EntityID = tsc.EntityID
join	tbEntityitem ei with (nolock)
	on vei.EntityID = ei.EntityID
	and vei.ItemID = ei.ItemID
join	tbItemQty iq with (nolock)
	on vei.EntityID = iq.EntityID
	and vei.ItemID = iq.ItemID
where	vei.VendorCode = @VendorCode and vei.EntityID = @Entityid and vei.VendorID = @VendorId and vei.DiscontinueDateOverrideEnabled = 1 
order by tsc.StockCountDate desc

