With Temp As
(SELECT InventoryItems.Name,AVG(InventoryTransactions.Price) as 'Average Cost',
PeriodicConsumptionItems.PhysicalInventory as 'In Stock',
AVG(InventoryTransactions.Price) *PeriodicConsumptionItems.PhysicalInventory AS 'Inventory Stock Average Value'
FROM InventoryItems
left join InventoryTransactions on InventoryItems.Id=InventoryTransactions.InventoryItem_Id
left join PeriodicConsumptionItems on InventoryItems.Id=PeriodicConsumptionItems.InventoryItemId
where PhysicalInventory>0
and PeriodicConsumptionItems.WarehouseConsumptionId=
(SELECT TOP 1 PeriodicConsumptions.Id
FROM PeriodicConsumptions
ORDER BY PeriodicConsumptions.Id DESC)
Group by InventoryItems.Id,PeriodicConsumptionItems.PhysicalInventory,InventoryItems.Name)
SELECT InventoryItems.Name,AVG(InventoryTransactions.Price) as 'Average Cost',
PeriodicConsumptionItems.PhysicalInventory as 'In Stock',
AVG(InventoryTransactions.Price) *PeriodicConsumptionItems.PhysicalInventory AS 'Inventory Stock Average Value'
FROM InventoryItems
left join InventoryTransactions on InventoryItems.Id=InventoryTransactions.InventoryItem_Id
left join PeriodicConsumptionItems on InventoryItems.Id=PeriodicConsumptionItems.InventoryItemId
where PhysicalInventory>0
and PeriodicConsumptionItems.WarehouseConsumptionId=
(SELECT TOP 1 PeriodicConsumptions.Id
FROM PeriodicConsumptions
ORDER BY PeriodicConsumptions.Id DESC)
Group by InventoryItems.Id,PeriodicConsumptionItems.PhysicalInventory,InventoryItems.Name