在SSRS中汇总表达式

时间:2015-04-24 13:37:02

标签: reporting-services ssrs-2008-r2

我有一个表达式来计算文本框中的值。在报告的最后,我想在该文本框中总共包含所有字段。但是,表达式使用查询中的命名字段,这些字段也是总计的。

=SUM(Fields!Total_Defective__.Value) / SUM(Fields!sales_Dollars.Value)

Total_Defective__和sales_Dollars在报告结尾处总计。因此,当我将该表达式放在该列的总字段中时,它会对其他列的总计执行计算。这会产生不正确的结果,因为它基于其他字段的总数。我要做的就是总结一下。我试过了Use a summary field in an expression in SSRS reports,但这只给了我一行以上的行,你不能用它汇总。

所以报告看起来像这样:

enter image description here

COQ总列数占有缺陷$ /销售额的总和。我希望它将列(COQ)添加到$ 0.39

以下是查询的内容:

    --Prod Qty
SELECT Division as 'division', SUM(prodtable.qtysched)as 'qty',rtrim(Ltrim(itemgroupid)) as 'itemGroup'
into #SCHED
FROM MiscReportTables.dbo.PlantDivisions 
inner join prodtable on prodtable.dimension2_ = MiscReportTables.dbo.PlantDivisions.Division  
WHERE PlantID IN (@plantid)
and SCHEDDATE between @start and @end   
Group by itemgroupid,MiscReportTables.dbo.PlantDivisions.Division

--sales qty
Select Division as 'Division', SUM(SALESQTY) as 'salesQTY',rtrim(Ltrim(salesline.itemgroupid)) as 'itemGroup'
into #salesQty
FROM MiscReportTables.dbo.PlantDivisions 
inner join prodtable on prodtable.dimension2_ = MiscReportTables.dbo.PlantDivisions.Division
inner join SalesLine on SalesLine.InventrefId = ProdTable.ProdiD
WHERE PlantID IN (@plantid)
and SCHEDDATE between @start and @end
Group By Division,salesLine.itemgroupid

--SALES Dollars
Select Division as 'Division',
 (SUM(SALESQTY) - REMAINSALESFINANCIAL) * (SalesPrice / PriceUnit) as 'sales$',
 rtrim(Ltrim(salesline.itemgroupid)) as 'itemGroup'
into #salesDollars
FROM MiscReportTables.dbo.PlantDivisions 
inner join prodtable on prodtable.dimension2_ = MiscReportTables.dbo.PlantDivisions.Division
inner join SalesLine on SalesLine.InventrefId = ProdTable.ProdiD
WHERE PlantID IN (@plantid)
and SCHEDDATE between @start and @end
Group By Division,salesLine.itemgroupid,REMAINSALESFINANCIAL,SalesPrice,PriceUnit


SELECT
    dbo.TQMNCR.NCRID,
    dbo.TQMPlantTable.PlantName AS 'Division',
    RTRIM(LTRIM(dbo.INVENTTABLE.ITEMGROUPID)) AS 'Item Process/Group',
    dbo.TQMNCRDEFECTTYPECODES.QTY AS 'Defective Qty',
    CASE CATYPE
        WHEN 0 THEN 
            (CASE WHEN dbo.SALESLINE.SALESID = ''
                THEN ISNULL((PRICE * (PERCENTEXT / 100))  / NULLIF(dbo.INVENTTABLEMODULE.PRICEUNIT, 0), 0) * dbo.TQMNCRDEFECTTYPECODES.QTY
                ELSE ISNULL((SALESPRICE * (PERCENTEXT / 100))  / NULLIF(dbo.SALESLINE.PRICEUNIT, 0), 0) * dbo.TQMNCRDEFECTTYPECODES.QTY END)
        WHEN 2 THEN 
            (CASE WHEN dbo.TQMNCR.SALESID = ''
                THEN ISNULL((PRICE * (PERCENTINT / 100))  / NULLIF(dbo.INVENTTABLEMODULE.PRICEUNIT, 0), 0) * dbo.TQMNCRDEFECTTYPECODES.QTY
                ELSE ISNULL((SALESPRICE * (PERCENTINT / 100))  / NULLIF(dbo.SALESLINE.PRICEUNIT, 0), 0) * dbo.TQMNCRDEFECTTYPECODES.QTY END)
        ELSE 0 END AS 'Total Defective $',
    dbo.PRODTABLE.PRODPOOLID
    ,#SCHED.qty,
    (#SCHED.qty - dbo.TQMNCRDEFECTTYPECODES.QTY) / #SCHED.qty as 'First Pass Yield',
    #salesQty.salesQty as 'Sales Quantity',
    #salesDollars.sales$ as 'sales Dollars'
FROM
    dbo.TQMNCR LEFT OUTER JOIN
    dbo.TQMDISPOSITION ON dbo.TQMNCR.DISPOSITIONID = dbo.TQMDISPOSITION.DISPOSITIONID LEFT OUTER JOIN 
    dbo.TQMCA_TABLE ON dbo.TQMCA_TABLE.NCRID = dbo.TQMNCR.NCRID LEFT OUTER JOIN 
    dbo.TQMNCRDEFECTTYPECODES ON dbo.TQMNCR.NCRID = dbo.TQMNCRDEFECTTYPECODES.NCRID LEFT OUTER JOIN
    dbo.TQMPlantTable ON TQMPlantTable.PlantID  = dbo.TQMNCR.PlantID LEFT OUTER JOIN 
    dbo.INVENTTABLE ON dbo.TQMNCR.ITEMID = dbo.INVENTTABLE.ITEMID LEFT OUTER JOIN 
    dbo.INVENTTABLEMODULE ON dbo.INVENTTABLE.ITEMID = dbo.INVENTTABLEMODULE.ITEMID AND MODULETYPE = 2 LEFT OUTER JOIN 
    dbo.SALESLINE ON dbo.SALESLINE.SALESID = dbo.TQMNCR.SALESID AND dbo.SALESLINE.ITEMID = dbo.TQMNCR.ITEMID LEFT OUTER JOIN 
    dbo.PRODTABLE ON dbo.TQMNCR.PRODID = dbo.PRODTABLE.PRODID
    inner join #sched on #sched.itemGroup = INVENTTABLE.itemgroupid
    inner join #salesQty on #salesQty.itemGroup = INVENTTABLE.itemgroupid
    inner join #salesDollars on  #salesDollars.itemgroup = INVENTTABLE.itemgroupid
WHERE
    SCHEDDATE between @start and @end
    AND
    dbo.TQMNCR.PlantID IN (@plantid)
ORDER BY dbo.INVENTTABLE.ITEMGROUPID, dbo.TQMPlantTable.PlantName


drop table #sched
drop table #SalesQty
drop table  #salesDollars

以下是结果集的样子: enter image description here

报告按Item_Process,Division,Total_Defective

分组

3 个答案:

答案 0 :(得分:2)

您可以尝试将 COQ 字段添加为数据集中的新计算字段。执行此操作将在数据集级别执行计算,而不是Tablix分组级别。

Adding a calculated field to a dataset

假设您已将此新字段称为“COQ”。

然后在您的总行中,将表达式 Sum(Fields!COQ.Value)作为总计。

这应首先对每行的COQ进行计算,然后将结果与总行相加。

编辑:如果您的数据集与您的表格完全一致,则上述操作将起作用。如果不是这种情况(正如您所指出的那样)并且SSRS中的表实际上是从更精细的数据集中分组的,则可以使用以下表达式对您的组进行总计:

=Sum(IIF(Sum(Fields!sales_Dollars.Value,"YOURGROUPNAME") > 0,   
 Sum(Fields!Total_Defective__.Value,"YOURGROUPNAME") / 
 Sum(Fields!sales_Dollars.Value,"YOURGROUPNAME"), 0))

这应该评估GROUP BY的每个成员中的计算,然后在整个表/数据集中计算计算结果。

答案 1 :(得分:1)

您应该可以使用

执行此操作
=SUM(Fields!Total_Defective__.Value/Fields!sales_Dollars.Value)

因此,它除以每个值,然后求和,而不是在除以之前对值求和。

编辑:如果你得到零除错误,你可以将以下内容添加到报告 - 报告属性 - 代码:

Public Function Quotient(ByVal numerator As Decimal, denominator As Decimal) As Decimal
    If denominator = 0 Then
        Return 0
    Else
        Return numerator / denominator
    End If
End Function

(参考:http://williameduardo.com/development/ssrs/ssrs-divide-by-zero-error/

输入公式为

=SUM(code.Quotient(Fields!Total_Defective__.Value,Fields!sales_Dollars.Value)

然后,您应该在表达式中使用相对较短的代码获得所需的结果。

答案 2 :(得分:1)

我相信你的要求超出了SSRS的能力范围。我会将分组移动到SQL数据集和(在分组到示例中的9个“细节”行之后),我将在该数据集中计算COQ,例如。

SELECT *
, Total_Defective / Sales AS COQ
FROM (
  SELECT 
    MyGroupColumns
    , SUM ( Total_Defective ) AS Total_Defective 
    , SUM ( Sales ) AS Sales 
   FROM  MyTable
  GROUP BY
    MyGroupColumns
  ) D1

然后SSRS可以只显示数据集输出的9行的详细COQ,并且您可以使用SSRS Sum()函数获取总行的COQ。