SQL Report Builder:对 ReportItem 使用聚合函数

SQL Report Builder: Use aggregate function on ReportItem

我为给定的单元格输入了以下表达式,它基本上是美元值除以数量得到每加仑美分值,标记为 Textbox41:

=ReportItems!Total_Gross_Profit2.Value / ReportItems!Gallon_Qty3.Value

我试图做的是将此表达式用于另一个单元格中的 AVG 聚合 =avg(ReportItems!Textbox41.Value),但出现错误:

The Value expression for the textrun 'Textbox79.Paragraphs[0].TextRuns[0]' uses an aggregate function on a report item. Aggregate functions can be used only on report items contained in page headers and footers.

是否存在某些不允许对 ReportItems 进行聚合的限制?我还尝试了以下方法,但也没有用:

=AVG(ReportItems!Total_Gross_Profit2.Value / ReportItems!Gallon_Qty3.Value)

我哪里错了?

关于您的问题:

Is there some limitation that does not allow aggregations on ReportItems?

您在提供的错误消息中有答案。

至于解决方案,很难根据您提供的信息给出精确的指导,但总的来说,开始考虑数据集字段而不是报表对象。如果您在矩阵或 table 内部操作,并且 'Total_Gross_Profit' 和 'Gallon_Qty_3' 的值看起来类似于此:

= ReportItems!ProfitsFromX.Value + ReportItems!ProfitsFromY.Value
= ReportItems!GallonQtyA.Value + ReportItems!GallonQtyB.Value

改为直接指向字段:

= Fields!ProfitsFromX.Value + Fields!ProfitsFromY.Value
= Fields!GallonQtyA.Value + Fields!GallonQtyB.Value

这样,聚合的时候就更清楚了:

= avg(
      (Fields!ProfitsFromX.Value + Fields!ProfitsFromY.Value)
    / (Fields!GallonQtyA.Value + Fields!GallonQtyB.Value)
)

如果您觉得这很麻烦,您可以在数据集对象上创建计算字段,并在适当的地方引用它们。