如何修改 SSRS 矩阵报表以显示总计的平均值?
How to Modify an SSRS Matrix Report to Display Averages of Totals?
我正在使用 SSRS 2016 构建涉及矩阵的报表对象。我希望我的行在最右边求和,但我的列需要是给定组中值的平均值。我想在 SSRS 中执行此操作,而不是在 TSQL 中进行聚合。到目前为止,我的报告如下所示:
预期结果是第一列数据平均为 2,接下来的 2 列平均为 1。
驱动报告的SQL如下:
DECLARE @year int = 2021
DECLARE @hpcode varchar(200) = 'BB'
/*Internal variables*/
DECLARE @month int = 1
DECLARE @monthend int = 12
DECLARE @table table (MEMB_KEYID UNIQUEIDENTIFIER, MEMBID varchar(50), RLSHIP varchar(2),
RELATIONSHIP varchar(50), HPCODE VARCHAR(10), OPT VARCHAR(25), EMPGROUP VARCHAR(40),
COMPANY_ID VARCHAR(10), MONTHDATE date)
/*If current year is selected, only grab up to current month*/
IF (YEAR(GETDATE()) = @year)
BEGIN
SET @monthend = MONTH(GETDATE())
END
WHILE @month<=@monthend
BEGIN
INSERT INTO @table (MEMB_KEYID, MEMBID, RLSHIP, RELATIONSHIP, HPCODE, OPT, EMPGROUP, COMPANY_ID, MONTHDATE)
SELECT MHP.MEMB_KEYID, MMV.MembID, mmv.rlship,
CASE
when mmv.RLSHIP = '18' then 'Subscriber'
when mmv.RLSHIP in ('01','53') then 'Spouse'
else 'Child/Other Dependent' end as RELATIONSHIP,
MHP.HPCODE, MHP.OPT, EMPGROUP, MHP.COMPANY_ID, DATEFROMPARTS(@year,@month,1)
FROM MEMB_HPHISTS MHP
INNER JOIN MEMB_MASTER_V MMV on MMV.MEMB_KEYID = MHP.MEMB_KEYID
WHERE ISNULL(MHP.OPTHRUDT, DATEFROMPARTS(@year,@month,1) ) >= DATEFROMPARTS(@year,@month,1)
and MHP.OPFROMDT <= EOMONTH(DATEFROMPARTS(@year,@month,1))
AND MHP.HPCODE in (@hpcode)
and mmv.MembID NOT LIKE 'TEMP%'
/*Increment @month*/
SET @month = @month+1
END
/*Grab data*/
SELECT Memb_KEYID, MembID, RLSHIP, RELATIONSHIP, HPCode, OPT, EMPGROUP, COMPANY_ID, MonthDate
from @Table
我的报告布局是
当我尝试 运行 报告时,抛出错误:
The value expression for the text box txtOPTAverageCount has a scope parameter that is not valid for an aggregate function.
如何完成此类摘要?
如果没有看到报表设计,很难判断,但假设您有一个按日期分组的行组,我们还假设此行组称为 grpDate
。
最后我假设主要数据单元格有一个类似于 =COUNT(Fields!MembID.Value)
的表达式
你需要做的是获取每个行组的总和并取平均结果,像这样...
=AVG(COUNT(Fields!MembID.Value, "grpDate"))
注意行组名必须用引号括起来,并且区分大小写
我正在使用 SSRS 2016 构建涉及矩阵的报表对象。我希望我的行在最右边求和,但我的列需要是给定组中值的平均值。我想在 SSRS 中执行此操作,而不是在 TSQL 中进行聚合。到目前为止,我的报告如下所示:
预期结果是第一列数据平均为 2,接下来的 2 列平均为 1。
驱动报告的SQL如下:
DECLARE @year int = 2021
DECLARE @hpcode varchar(200) = 'BB'
/*Internal variables*/
DECLARE @month int = 1
DECLARE @monthend int = 12
DECLARE @table table (MEMB_KEYID UNIQUEIDENTIFIER, MEMBID varchar(50), RLSHIP varchar(2),
RELATIONSHIP varchar(50), HPCODE VARCHAR(10), OPT VARCHAR(25), EMPGROUP VARCHAR(40),
COMPANY_ID VARCHAR(10), MONTHDATE date)
/*If current year is selected, only grab up to current month*/
IF (YEAR(GETDATE()) = @year)
BEGIN
SET @monthend = MONTH(GETDATE())
END
WHILE @month<=@monthend
BEGIN
INSERT INTO @table (MEMB_KEYID, MEMBID, RLSHIP, RELATIONSHIP, HPCODE, OPT, EMPGROUP, COMPANY_ID, MONTHDATE)
SELECT MHP.MEMB_KEYID, MMV.MembID, mmv.rlship,
CASE
when mmv.RLSHIP = '18' then 'Subscriber'
when mmv.RLSHIP in ('01','53') then 'Spouse'
else 'Child/Other Dependent' end as RELATIONSHIP,
MHP.HPCODE, MHP.OPT, EMPGROUP, MHP.COMPANY_ID, DATEFROMPARTS(@year,@month,1)
FROM MEMB_HPHISTS MHP
INNER JOIN MEMB_MASTER_V MMV on MMV.MEMB_KEYID = MHP.MEMB_KEYID
WHERE ISNULL(MHP.OPTHRUDT, DATEFROMPARTS(@year,@month,1) ) >= DATEFROMPARTS(@year,@month,1)
and MHP.OPFROMDT <= EOMONTH(DATEFROMPARTS(@year,@month,1))
AND MHP.HPCODE in (@hpcode)
and mmv.MembID NOT LIKE 'TEMP%'
/*Increment @month*/
SET @month = @month+1
END
/*Grab data*/
SELECT Memb_KEYID, MembID, RLSHIP, RELATIONSHIP, HPCode, OPT, EMPGROUP, COMPANY_ID, MonthDate
from @Table
我的报告布局是
The value expression for the text box txtOPTAverageCount has a scope parameter that is not valid for an aggregate function.
如何完成此类摘要?
如果没有看到报表设计,很难判断,但假设您有一个按日期分组的行组,我们还假设此行组称为 grpDate
。
最后我假设主要数据单元格有一个类似于 =COUNT(Fields!MembID.Value)
你需要做的是获取每个行组的总和并取平均结果,像这样...
=AVG(COUNT(Fields!MembID.Value, "grpDate"))
注意行组名必须用引号括起来,并且区分大小写