我有一个过程,旨在使用SSRS为人员列表构建数字报告,其中列A是他们的姓名,列B和C是他们今年迄今为止的数字(从1月份开始到例如5月份),列D和E是去年同期月份的相同数字。下面是该过程的示例。此外,我已更改名称和关键数据,以使其更易于理解,但这可能意味着我无法立即发现某些地方存在轻微的拼写错误。每个参数的数据示例都作为已注释的行放置在下面。
这项任务的重点在于,应该能够检索某一年月份的报告,并查看从该年1月至所选月份所累积的数字,然后将其与前一年的同一段时间进行比较。
它使用左外连接,因为有些员工是新员工,去年没有任何数据。
当我在SQL Server Management Studio中执行此过程时,它完全运行良好。我得到了所有正确的数据和包括没有去年数据的新员工在内的员工列表。
当我在SSRS中执行过程时,我只得到一个员工列表,其中只包括今年和去年都有数据的员工。几乎就像左外连接被作为内连接运行一样。
这是什么原因引起的?
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月至所选月份所累积的数字,然后将其与前一年的同一段时间进行比较。
它使用左外连接,因为有些员工是新员工,去年没有任何数据。
当我在SQL Server Management Studio中执行此过程时,它完全运行良好。我得到了所有正确的数据和包括没有去年数据的新员工在内的员工列表。
当我在SSRS中执行过程时,我只得到一个员工列表,其中只包括今年和去年都有数据的员工。几乎就像左外连接被作为内连接运行一样。
这是什么原因引起的?
GROUP BY fk_resource、name、TEMP.sum_amount_last_year、TEMP.avg_amount_last_year
。 - vercelliGROUP BY fk_resource
属于#lastyearsnumbers
还是datamart.numbers_month
? - vercelli