逐行报告服务器聚合

时间:2009-03-02 11:06:59

标签: sql-server reporting-services

也许这是一个新手问题,但

想象一下,我有一份报告显示销售订单清单pr sales-rep,已逐项列出 到sku级别,并希望显示1个sku的总销售额的百分之几,销售代表已售出。

  

销售人

     
      
  • 订单清单

         - List of items              no sold:  5  out of this months total 942
    
  •   

示例:

  

John Doe

- Order #12312

      - SKU SP1231     Sold 5 . Month total 445

      - SKU SP4141     Sold 63 .  Month total 300
      Emma Doe

- Order #123324

      - SKU SP1231     Sold 65 . Month total 445

      - SKU SP4141     Sold 2 .  Month total 300
     

月份总数是指总报告期内该特定sku的销售数量。

如何添加此号码?如果我使用Fields!TotalAmount.Value,它会将总数作为组的总数。即销售代表Z在订单X中销售了多少sku Y.

我需要该特定SKU的全球销售总额。

如果我说SUM(Fields!Amount,Nothing)设置全局范围,它会给出所有sku的总和,而不仅仅是所讨论的sku。

我该怎么做?

修改

报表服务器是SSRS,报表使用共享数据源,该数据源是报表服务器上已托管的报表模型,指向SQL Server数据库 内容。

2 个答案:

答案 0 :(得分:0)

您没有说明您正在使用的DBMS(而不是来自Fields!Amount语法的Oracle)。这适用于您的DBMS吗?:

with sku_sales as
( select sku, sum(value) as sku_value
  from   sales
  group by sku
)
select sales.salesperson, sum(sales.value), sku_sales.sku_value
from   sales
join   sku_sales on sku_sales.sku = sales.sku
group by sales.salesperson, sku_sales.sku_value

答案 1 :(得分:0)

我该怎么做才是使用以逗号分隔的列表在报告数据集中选择Sku总和:

-- 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

然后你可以使用一些自定义代码来通过sku ID检索总和值(我现在必须用C#编写,你可以轻松地将它转换为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;
}

然后在文本框中使用它值表达式:

=Code.GetSkuSum(Fields!SkuIDs.Value,Fields!SkuSums.Value,Fields!SkuID.Value)