如何在 SQL 中的聚合函数中引用 SSRS 报告参数

How do I reference a SSRS report parameter in an aggregate function in SQL

我有一个 SSRS 报告,它总结了指定日期范围内的数据集。

我想使用报告参数为查询中的聚合函数提供日期范围。这可能吗?

我有使用变量的聚合函数,但我想更改它以便用户可以 select 报告之前的日期范围是 运行。

下面是原始的工作聚合函数(在整个查询的上下文中 - 我删除了查询的非相关部分以避免混淆)。

请注意,@MonthNum 和@ReportYear 变量被用作 COUNT() 函数的条件。我想更改这些变量以报告参数值,但无法解决这个问题。

DECLARE @MonthNum int;
DECLARE @ReportYear int;

SET @MonthNum = CASE WHEN (MONTH(GETDATE())=1) THEN 12 ELSE MONTH(GETDATE()) - 1 END;
SET @ReportYear = CASE WHEN (MONTH(GETDATE())=1) THEN 
YEAR(GETDATE())-1 ELSE YEAR(GETDATE()) END;

SELECT
COUNT(CASE WHEN (MONTH(tblInvoices.fldDeliveredDate) = @MonthNum AND
(YEAR(tblInvoices.fldDeliveredDate)) = @ReportYear) THEN tblSessions.fldStudioNo END) 
AS MonthVolume

FROM tblInvoices INNER JOIN
tblSessions ON tblInvoices.fldSesID = tblSessions.fldSesID INNER JOIN
tblSessionCustom ON tblSessions.fldSesID = tblSessionCustom.fldSessionCustomID 
INNER JOIN
tblCompInfo ON tblSessions.fldStudioNo = tblCompInfo.fldStudioNo

GROUP BY tblSessions.fldStudioNo, tblCompInfo.fldCompName
HAVING (NOT (tblSessions.fldStudioNo LIKE '999'))

提前感谢您提供的任何帮助或指导。非常感谢。

戴夫

我相信您需要类似以下内容,您需要在 SSRS 中创建参数 @StartDate 和 @EndDate,以便将它们输入到查询中:

DECLARE @MonthNum int;
DECLARE @ReportYear int,
    @StartDate date,
    @EndDate date

SET @MonthNum = CASE WHEN (MONTH(GETDATE())=1) THEN 12 ELSE MONTH(GETDATE()) - 1 END;
SET @ReportYear = CASE WHEN (MONTH(GETDATE())=1) THEN 
YEAR(GETDATE())-1 ELSE YEAR(GETDATE()) END;

SELECT
COUNT(CASE WHEN (MONTH(tblInvoices.fldDeliveredDate) = @MonthNum AND
(YEAR(tblInvoices.fldDeliveredDate)) = @ReportYear) THEN tblSessions.fldStudioNo END) 
AS MonthVolume

FROM tblInvoices INNER JOIN
tblSessions ON tblInvoices.fldSesID = tblSessions.fldSesID INNER JOIN
tblSessionCustom ON tblSessions.fldSesID = tblSessionCustom.fldSessionCustomID 
INNER JOIN
tblCompInfo ON tblSessions.fldStudioNo = tblCompInfo.fldStudioNo
Where tblInvoices.fldDeliveredDate between @StartDate and @EndDate

GROUP BY tblSessions.fldStudioNo, tblCompInfo.fldCompName
HAVING (NOT (tblSessions.fldStudioNo LIKE '999'))