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

时间:2015-01-20 12:00:03

标签: sql reporting-services parameters sql-server-2012 aggregate-functions

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

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

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

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

注意@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'))

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

戴夫

1 个答案:

答案 0 :(得分:0)

我相信您需要以下内容,您需要在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'))