What would I do is select Sku total sum in your report dataset using comma separated lists:
-- testing data
DECLARE @Order TABLE (ID INT, SalesRepID INT, Date DATETIME)
INSERT INTO @Order
SELECT 1, 1, GETDATE() UNION
SELECT 2, 2, GETDATE() UNION
SELECT 3, 1, GETDATE() UNION
SELECT 4, 1, GETDATE() UNION
SELECT 5, 2, GETDATE()
DECLARE @OrderDetail TABLE (ID INT, OrderID INT, SkuID INT, SkuCount INT)
INSERT INTO @OrderDetail
SELECT 1, 1, 1, 10 UNION
SELECT 2, 1, 2, 5 UNION
SELECT 3, 1, 3, 20 UNION
SELECT 4, 1, 4, 10 UNION
SELECT 5, 2, 1, 15 UNION
SELECT 6, 2, 2, 25 UNION
SELECT 7, 2, 3, 15 UNION
SELECT 8, 3, 1, 15 UNION
SELECT 9, 3, 1, 10 UNION
SELECT 10, 3, 3, 10 UNION
SELECT 11, 3, 4, 15 UNION
SELECT 12, 4, 1, 5
DECLARE @Sku TABLE (ID INT, SkuCode VARCHAR(10))
INSERT INTO @Sku
SELECT 1, 'SP1233' UNION
SELECT 2, 'SP2262' UNION
SELECT 3, 'SP1531' UNION
SELECT 4, 'SP4235'
DECLARE @SalesRep TABLE (ID INT, SalesRepName VARCHAR(20))
INSERT INTO @SalesRep
SELECT 1, 'John Doe' UNION
SELECT 2, 'Emma Doe'
-- filters for testing
DECLARE @StartDate DATETIME, @EndDate DATETIME
SELECT @StartDate = GETDATE(), @EndDate = GETDATE()
DECLARE @SkuIDList VARCHAR(8000), @SkuSumList VARCHAR(8000)
SELECT @SkuIDList = '', @SkuSumList = ''
--gether all sku IDs and Sum in two comma separated list
SELECT @SkuIDList = @SkuIDList + CONVERT(VARCHAR, OD.SkuID) + ',',
@SkuSumList = @SkuSumList + CONVERT(VARCHAR, SUM(OD.SkuCount)) + ','
FROM @Order O
INNER JOIN @OrderDetail OD ON O.ID = OD.OrderID
WHERE O.Date BETWEEN @StartDate AND @EndDate
GROUP BY OD.SkuID
-- remove last ','
SELECT @SkuIDList = SUBSTRING(@SkuIDList, 0, LEN(@SkuIDList)),
@SkuSumList = SUBSTRING(@SkuSumList, 0, LEN(@SkuSumList))
-- include thouse lists in the main select for your report dataset
SELECT O.ID, OD.SkuID, O.SalesRepID, SR.SalesRepName, S.SkuCode,
OD.SkuCount, @SkuIDList AS SkuIDs, @SkuSumList AS SkuSums
FROM @Order O
INNER JOIN @OrderDetail OD ON O.ID = OD.OrderID
INNER JOIN @Sku S ON OD.SkuID = S.ID
INNER JOIN @SalesRep SR ON O.SalesRepID = SR.ID
WHERE O.Date BETWEEN @StartDate AND @EndDate
Then you can use some custome code to retrieve sum value by sku ID (I have to write in C# currently, you easely convert it to VB):
public int GetSkuSum(string skuSumCSV, string skuIDCSV, int searchSkuID)
{
string[] strSkuSum = skuSumCSV.Split(',');
string[] strSkuID = skuIDCSV.Split(',');
for (int i = 0; i < strSkuID.Length; i++)
{
if (Convert.ToInt32(strSkuID[i].Trim()) == searchSkuID)
{
return Convert.ToInt32(strSkuSum[i]);
}
}
return 0;
}
Then use it in your textbox Value expression:
=Code.GetSkuSum(Fields!SkuIDs.Value,Fields!SkuSums.Value,Fields!SkuID.Value)