Here is the inner query,
Code:
SELECT T1.ClientID, T1.ProductID, T1.MonthlyDate,
(SELECT MAX(T3.MonthlyDate)
FROM tblPositions AS T3
WHERE T3.ClientID = T1.ClientID
AND T3.MonthlyDate < T1.MonthlyDate) AS PriorDate,
(SELECT SUM(T2.TradePosition)
FROM tblPositions AS T2
WHERE T2.ClientID = T1.ClientID
AND T2.InvestmentID = T1.InvestmentID
AND T2.MonthlyDate <= T1.MonthlyDate) AS NetPosition,
T1.InvestmentID, tblTradingPrices.AdjClose AS LastMTM, [NetPosition]-[T1].[TradePosition] AS BegPosition, T1.TradePosition AS [Add/Sub Posn], T1.TradePrice AS [Add/Sub Price], T1.BrokerageFee, tblTradingPrices.AdjClose AS MTMPrice, T1.Rate, Abs(Round(IIf([Add/Sub Posn]<0,[Add/Sub Posn]*[Add/Sub Price]*Nz([SECRate],0),0),2)) AS [Fee], [LastMTM]*[BegPosition] AS [Asset Value Beg], [Add/Sub Posn]*[Add/Sub Price]+[BrokerageFee]+[Fee] AS [Assets Add/Sub], ([BegPosition]+[Add/Sub Posn])*[MTMPrice] AS EOMAssetValue, [EOMAssetValue]-[Assets Add/Sub]-[Asset Value Beg] AS [AssetsInc/Dec]
FROM tblTradingPrices
INNER JOIN tblPositions AS T1
ON (tblTradingPrices.InvestmentID = T1.InvestmentID)
AND (tblTradingPrices.MonthlyDate = T1.MonthlyDate)
ORDER BY T1.ClientID, T1.MonthlyDate, T1.InvestmentID;
Here is the Cross Tab:
Code:
TRANSFORM First(tblWeightings.Weightings) AS FirstOfWeightings
SELECT tblClients.LastName, tblWeightings.MonthlyDate, qryPositions.BegPosition
FROM ((tblInvestments
INNER JOIN tblWeightings
ON tblInvestments.InvestmentID = tblWeightings.InvestmentID)
INNER JOIN tblTradingPrices
ON (tblWeightings.MonthlyDate = tblTradingPrices.MonthlyDate)
AND (tblInvestments.InvestmentID = tblTradingPrices.InvestmentID))
INNER JOIN (tblClients INNER JOIN qryPositions
ON tblClients.ClientID = qryPositions.ClientID)
ON (qryPositions.InvestmentID = tblWeightings.InvestmentID)
AND (qryPositions.ProductID = tblWeightings.ProductID)
AND (qryPositions.MonthlyDate = tblWeightings.MonthlyDate)
AND (tblInvestments.InvestmentID = qryPositions.InvestmentID)
GROUP BY tblClients.LastName, tblWeightings.MonthlyDate, qryPositions.BegPosition
ORDER BY tblClients.LastName, tblWeightings.MonthlyDate
PIVOT tblInvestments.Investment;