I have a procedure that is supposed to build a report in SSRS with numbers for a list of people where column A is their name, column B and C are their numbers thus far this year (from January and up to for example May), and column D and E are the same numbers from the previous year in the same span of months. Below is what the procedure looks like. Also; I've altered the names and key data here to make it more understandable for you, but that might mean that there is a slight typo here and there that I don't immediately spot. Examples of what the parameter data may be are put as a commented out line below each parameter.
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;
The point of this task is that it should be possible to retrieve a report for a given month of a year and see what the accumulated numbers from january of that year and up to the selected month is, and compare it to the same period from the year before.
It uses a Left Outer Join because some employees are new and do not have any numbers from last year.
When I execute this procedure in SQL Server Management Studio, it runs completely fine. I get all the right data and a list of employees that also include the new employees that have no data for last year.
When I execute the procedure in SSRS, I get a list of employees that only cover the employees that have data both for this year as well as last year. It is almost as if the left outer join is run as an inner join.
What could cause this?
fk_resource
without aliases onWHERE
andGROUP BY
. Is that working? – vercelliGROUP BY fk_resource, name, TEMP.sum_amount_last_year, TEMP.avg_amount_last_year
– vercelliGROUP BY fk_resource
belongs to#lastyearsnumbers
or todatamart.numbers_month
? – vercelli