我有一个程序应该在 SSRS 中构建一个报告,其中包含人员列表的编号,其中 A 列是他们的名字,B 列和 C 列是他们今年到目前为止的编号(从 1 月到 5 月) ),D 列和 E 列与上一年相同月份的数字相同。下面是程序的样子。还;我已经更改了此处的名称和关键数据,以使您更容易理解,但这可能意味着这里和那里存在我没有立即发现的轻微错字。参数数据可能是什么的示例作为注释行放在每个参数下面。
ALTER PROCEDURE [dbo].[number_comparison]
@resource AS int
-- retrieves a specific employee, or NULL. NULL on this parameter will retrieve a list of all employees (or all within a given type if the employee_type parameter is set)
,@employee_Type AS nvarchar(20)
-- type will retrieve all employees of a certain type. NULL on this will retrieve all employees regardless of type
,@time AS nvarchar(6)
-- Comes in the form YYYYMM. Cannot be NULL.
AS
BEGIN
DECLARE @time_last_year nvarchar(6) = CONVERT(nvarchar(6), @time-100);
SELECT
fk_resource
,SUM(amount) AS sum_amount_last_year
,AVG(amount) AS avg_amount_last_year
INTO
#lastyearsnumbers
FROM
datamart.numbers_month
INNER JOIN
dvh.resources
ON
(resources.pk_resource = fk_resource)
AND
(resources.pk_ressurs != -1)
WHERE
(fk_time LIKE LEFT(@time_last_year,4)+'%')
AND
(@resource IS NULL OR fk_resource = @resource)
AND
(@employee_Type IS NULL OR employee_Type = @employeeType)
AND
(fk_time < ((@time_last_year)+1))
GROUP BY
fk_resource
SELECT
fk_resource
,name
,SUM(amount) AS sum_amount_this_year
,AVG(amount) AS avg_amount_this_year
,TEMP.sum_amount_last_year
,TEMP.avg_amount_last_year
FROM
datamart.numbers_month
INNER JOIN
dvh.resource
ON
(resource.pk_resource = fk_resource)
AND
(resource.pk_resource != -1)
LEFT OUTER JOIN
#lastyearsnumbers TEMP
ON TEMP.fk_resource = datamart.numbers_month.fk_resource
WHERE
(fk_time LIKE (LEFT(@time,4))+'%')
AND
(@resource IS NULL OR fk_resource = @resource)
AND
(@employee_Type IS NULL OR employee_Type = @employee_Type)
AND
(fk_time < (@time+1))
GROUP BY
fk_resource, name, TEMP.sum_amount_last_year, TEMP.avg_amount_last_year
END;
此任务的要点是应该可以检索一年中给定月份的报告,并查看从该年 1 月到所选月份的累计数字是多少,并将其与同一月份进行比较前一年的时期。
它使用 Left Outer Join,因为一些员工是新员工并且没有去年的任何数字。
当我在 SQL Server Management Studio 中执行此过程时,它运行得非常好。我获得了所有正确的数据和员工列表,其中还包括没有去年数据的新员工。
当我在 SSRS 中执行该过程时,我得到了一份员工列表,仅涵盖了拥有今年和去年数据的员工。左外连接几乎就像内连接一样运行。
这可能是什么原因造成的?
最佳答案
我记得在某个地方读过它(不记得在我脑海中),报告向导不喜欢临时表。当我得到它时,我会更新评论。但是,与此同时,您能否尝试使用 CTE(公用表表达式)代替临时表或使用任何暂存表来运行相同的查询?
我修改了您的脚本以使用 CTE,如下所示。
ALTER PROCEDURE [dbo].[number_comparison]
@resource AS int
-- retrieves a specific employee, or NULL. NULL on this parameter will retrieve a list of all employees (or all within a given type if the employee_type parameter is set)
,@employee_Type AS nvarchar(20)
-- type will retrieve all employees of a certain type. NULL on this will retrieve all employees regardless of type
,@time AS nvarchar(6)
-- Comes in the form YYYYMM. Cannot be NULL.
AS
BEGIN
DECLARE @time_last_year nvarchar(6) = CONVERT(nvarchar(6), @time-100);
WITH cte AS
(
SELECT
fk_resource
,SUM(amount) AS sum_amount_last_year
,AVG(amount) AS avg_amount_last_year
FROM
datamart.numbers_month
INNER JOIN
dvh.resources
ON
(resources.pk_resource = fk_resource)
AND
(resources.pk_ressurs != -1)
WHERE
(fk_time LIKE LEFT(@time_last_year,4)+'%')
AND
(@resource IS NULL OR fk_resource = @resource)
AND
(@employee_Type IS NULL OR employee_Type = @employeeType)
AND
(fk_time < ((@time_last_year)+1))
GROUP BY
fk_resource
)
SELECT
fk_resource
,name
,SUM(amount) AS sum_amount_this_year
,AVG(amount) AS avg_amount_this_year
,TEMP.sum_amount_last_year
,TEMP.avg_amount_last_year
FROM
datamart.numbers_month
INNER JOIN
dvh.resource
ON
(resource.pk_resource = fk_resource)
AND
(resource.pk_resource != -1)
LEFT OUTER JOIN
cte as TEMP
ON TEMP.fk_resource = datamart.numbers_month.fk_resource
WHERE
(fk_time LIKE (LEFT(@time,4))+'%')
AND
(@resource IS NULL OR fk_resource = @resource)
AND
(@employee_Type IS NULL OR employee_Type = @employee_Type)
AND
(fk_time < (@time+1))
GROUP BY
fk_resource, name, TEMP.sum_amount_last_year, TEMP.avg_amount_last_year
END;
关于sql-server - SQL 存储过程在 SQL Server Management Studio 中运行良好,但在 Reporting Services (SSRS) 中运行不佳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37721322/