Hi. I'm building a View join InventSum & InventDim on InventSum.InventDimId == InventDim.InventDimId.
This is the query results.

As you can see, this query is filtered by ItemId = 'YMGMY0090'. For one itemId there's multiple InventBatchId.
In the view, I want to display a column that calculates sum(PostedValue) / sum(PostedQty).
In this case, for batch number 32017, the calculation should return (5005.56 + 32.7185) / (59 + 1) = 83.9713.
How do I select sum(PostedValue) & sum(PostedQty) from InventSum table but group by InventBatchId from InventDim table?
I write this display method in InventDim table but it's getting random itemId instead of 'YMGMY0090' filtered when running this view:
public display Price unitCost()
{
InventDim inventDim;
InventSum inventsum;
Price price;
select sum(PostedValue), sum(PostedQty) from inventsum
join this
group by inventsum.ItemId, this.inventBatchId
where inventsum.inventDimId == this.InventDimId;
info(InventSum.ItemId);
info(inventDim.inventBatchId);
info(Num2Str(InventSum.PostedValue,0,5,1,3));
info(Num2Str(InventSum.PostedQty,0,5,1,3));
if(InventSum.PostedQty == 0)
{
price = 0;
}
else
{
price = InventSum.PostedValue / InventSum.PostedQty;
}
return price;
}
Thank You.