It appears I have managed to solve my own problem, it just took a little bit more research, lateral thinking, a lot of failed attempts and curse words (oh so many curse words!)
I toyed with the idea of adding extra steps in the delphi side of this program that would select from the cost prices table based upon the date range i needed and then re-write my original query to incorporate the new table joined in. However it's not as fun to solve a problem if you don't learn any new skills along the way ;-).
The answer: TVF- Table Valued Function. After a lot of research on alternative ways i stumbled across these TVF's. Further investigation seemed to reveal that because of the way the optimizer handles scalar functions as opposed to TVF's, they were tremendously quicker in certain applications so i decided to re-write my function as such:
CREATE FUNCTION dbo.CostAtDate ( @costdate AS datetime , @product AS int )
RETURNS table
AS
Return (
Select Top 1
BASE_InventoryCostLogDetail.AverageCostAfter
From
BASE_InventoryCostLogDetail
Where
CreatedDttm < @costdate And CreatedDttm > DATEADD(month,-1,@costDate) And
ProdId = @product
Order By
CreatedDttm Desc)
And instead of calling it the traditional way
dbo.CostAtDate(SO_SalesOrder.OrderDate, SO_SalesOrderInvoice_Line.ProdId)
I re-jigged all references of it in my query to:
(Select * from dbo.CostAtDate(SO_SalesOrder.OrderDate,
SO_SalesOrderInvoice_Line.ProdId))
Testing it i found a significant increase in performance (4sec for 65k+ records, as opposed to the previous function which would usually time out after a few minutes even though the expected resultset was ~10k records.)
I'm sure plenty of you know an even better way, but for the moment this works well....and i found it all by myself: kudos to me!