SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
CREATE PROCEDURE tp2.rsp_CoursesCompIncompRpt
@Start_Date smalldatetime,
@End_Date smalldatetime,
@Course_Code varchar(25),
@Super_User varchar(45),
@Business_Unit varchar(80) = NULL,
@Region varchar(255)= NULL
AS
--Create variables for "placeholders" for those employees that are not attached to a Course
DECLARE
@Course_Name varchar(255),
@Course varchar(255)
--Create Place holders for input parameters
IF @Region IS NULL
BEGIN
SET @Region = NULL
END
IF @Business_Unit IS NULL
BEGIN
SET @Business_Unit = NULL
END
--Strip off time from date
SET @Start_Date = CONVERT(CHAR(10),@Start_Date,101)
SET @End_Date = CONVERT(CHAR(10),@End_Date,101)
IF @Super_User = 'All - Summary'
BEGIN
SET @Region = NULL
SET @Business_Unit = NULL
END
--CREATE ALL TEMP TABLES NEEDED
CREATE TABLE #EMPLOYEES_INFO
(
EmployeeName varchar(100), --Last,First(#)EmployeeId varchar(60), --EmployeeId
JobTitle varchar(80), --Job Title abbr.
EmpStatus varchar(25), --Full or Part Time
Location varchar(80), --Location Code
CostCenter varchar(25), --Cost Center Code
Station_CostCenter varchar(50), --Loc-CostCenter
Region varchar(255) --Region Name
)
CREATE TABLE #COURSE_INFO
(
CourseId char(20), --Course ID
CourseName varchar(255), --Course Name
CourseCode varchar(25), --Course Code
Course varchar(255) --CourseCode/Name
)
CREATE TABLE #EMPLOYEE_COURSE_INFO
(
EmployeeId varchar(60), --EmployeeId
CostCenter varchar(25), --Cost Center Code
Location varchar(80), --Location Code
CourseId char(20), --Course ID
CourseName varchar(255), --Course Name
CourseCode varchar(25), --Course Code
Course varchar(255), --CourseCode/Name
CompletionDate smalldatetime --Date completed
)
CREATE TABLE #FINAL_RESULTS(
EmployeeName varchar(100), --Last,First(#)
EmployeeId varchar(60), --EmployeeId
JobTitle varchar(80), --Job Title abbr.
EmpStatus varchar(25), --Full or Part Time
Location varchar(80), --Location Code
CostCenter varchar(25), --Cost Center Code
Station_CostCenter varchar(50), --Loc-CostCenter
Region varchar(255), --Region Name
CourseName varchar(255), --Course Name
CourseCode varchar(25), --Course Code
Course varchar(255), --CourseCode/Name
CompletionDate smalldatetime, --Date completed
CompletedFlag char(1), --Y or N
Super_User varchar(45) --Detail/Summary
--Insert Data into temp table to hold the course info
INSERT INTO #COURSE_INFO
SELECT
Course.[id] CourseID,
substring(Course.title,1,80) CourseName,
Course.course_no CourseCode,
Course.course_no+' / '+
substring(Course.title,1,80) Course
FROM
tp2.tpv_pub_courses Course
WHERE Course.Course_no = @Course_Code
UNION ALL
SELECT
id CourseId, --ProductId
name CourseName, --ProductName,
Part_no CourseCode, --ProductCode,
Product.Part_no+' / '+
substring(Product.name,1,80) Course --Product
FROM
tp2.TPV_PUB_PRODUCT_CATALOG Product
WHERE Product.Part_no = @Course_Code
SET @Course_Name = (SELECT #COURSE_INFO.CourseName FROM #COURSE_INFO)
SET @Course = (SELECT #COURSE_INFO.Course FROM #COURSE_INFO)
/* ^^Sets the default course name and course (concatenated) for the query,
so that any employee who did NOT take the course is "assigned" a course name.
This ensures that NO employees at the given Region, Business Unit, etc. provided
by the input parameters are left out.*/
IF @Super_User = 'Regional - Summary'
BEGIN
/* Insert all employee information for Region in question */
INSERT INTO #EMPLOYEES_INFO
SELECT DISTINCT
(Employee.lname+', '
+Employee.fname)+' ('
+RTRIM(Employee.employee_no)+')' EmployeeName,
RTRIM(Employee.employee_no) EmployeeId,
ISNULL(JobType.[name],'') JobTitle,
ISNULL(Employee.[Status],'') EmpStatus,
ISNULL(Location.loc_name,'') Location,
ISNULL(BusinessUnit.cost_center,'')CostCenter,
ISNULL(BusinessUnit.name2,'') Station_CostCenter,
ISNULL(BusinessUnit.custom1,(
RTRIM(LEFT(BusinessUnit.name2,
(charindex('-',BusinessUnit.name2)-2))))) Region
FROM tp2.tpt_employees Employee
INNER JOIN
tp2.tpt_job_type JobType
ON Employee.jobtype_id = JobType.[id]
INNER JOIN
tp2.tpt_company BusinessUnit
ON Employee.company_id = BusinessUnit.[id]
INNER JOIN
tp2.tpt_locations Location
ON Employee.location_id = Location.[id]
WHERE Employee.Status <> 'Terminated'
AND JobType.job_type = 0
AND (RTRIM(BusinessUnit.custom1) = @Region)
/*Insert employee id's of those who have completed the course in
question at the specified Region*/
INSERT INTO #EMPLOYEE_COURSE_INFO
SELECT DISTINCT
RTRIM(Employee.employee_no) EmployeeID,
BusinessUnit.cost_center CostCenter,
Location.loc_name Location,
Course.CourseId CourseId,
Course.CourseName CourseName,
Course.CourseCode CourseCode,
Course.Course Course,
CourseCompleted.Acquired_on CompletionDate
FROM tp2.tpt_employees Employee
INNER JOIN
tp2.tpt_company BusinessUnit
ON Employee.company_id = BusinessUnit.[id]
INNER JOIN
tp2.tpt_locations Location
ON Employee.location_id = Location.[id]
INNER JOIN
tp2.tpv_pub_ed_prod_completed CourseCompleted
ON Employee.[id] = CourseCompleted.student_id
INNER JOIN
#COURSE_INFO Course
ON
CourseCompleted.product_id = Course.CourseId
WHERE (CONVERT(CHAR(10),CourseCompleted.Acquired_on,101)>= @Start_date
AND CONVERT(CHAR(10),CourseCompleted.Acquired_on,101) <= @End_Date)
AND Employee.Status <> 'Terminated'
AND Course.CourseCode = @Course_Code
AND (RTRIM(BusinessUnit.custom1) = @Region)
END
ELSE
/* Insert all employee information for Business Unit in question */
INSERT INTO #EMPLOYEES_INFO
SELECT DISTINCT
(Employee.lname+', '
+Employee.fname)+' ('
+RTRIM(Employee.employee_no)+')' EmployeeName,
RTRIM(Employee.employee_no) EmployeeId,
ISNULL(JobType.[name],'') JobTitle,
ISNULL(Employee.[Status],'') EmpStatus,
ISNULL(Location.loc_name,'') Location,
ISNULL(BusinessUnit.cost_center,'')CostCenter,
ISNULL(BusinessUnit.name2,'') Station_CostCenter,
ISNULL(BusinessUnit.custom1,(
RTRIM(LEFT(BusinessUnit.name2,(charindex('-',BusinessUnit.name2)-2))))
) Region
FROM tp2.tpt_employees Employee
INNER JOIN
tp2.tpt_job_type JobType
ON Employee.jobtype_id = JobType.[id]
INNER JOIN
tp2.tpt_company BusinessUnit
ON Employee.company_id = BusinessUnit.[id]
INNER JOIN
tp2.tpt_locations Location
ON Employee.location_id = Location.[id]
WHERE Employee.Status <> 'Terminated'
AND JobType.job_type = 0
AND ((@Business_Unit IS NULL)
OR (Location.loc_name = @Business_Unit))
/*Insert employee id's of those who have completed the course in
question at the specified Business Unit*/
INSERT INTO #EMPLOYEE_COURSE_INFO
SELECT DISTINCT
RTRIM(Employee.employee_no) EmployeeID,
BusinessUnit.cost_center CostCenter,
Location.loc_name Location,
Course.CourseId CourseId,
Course.CourseName CourseName,
Course.CourseCode CourseCode,
Course.Course Course,
CourseCompleted.Acquired_on CompletionDate
FROM tp2.tpt_employees Employee
INNER JOIN
tp2.tpt_company BusinessUnit
ON Employee.company_id = BusinessUnit.[id]
INNER JOIN
tp2.tpt_locations Location
ON Employee.location_id = Location.[id]
INNER JOIN
tp2.tpv_pub_ed_prod_completed CourseCompleted
ON Employee.[id] = CourseCompleted.student_id
INNER JOIN
#COURSE_INFO Course
ON CourseCompleted.product_id = Course.CourseId
WHERE (CONVERT(CHAR(10),CourseCompleted.Acquired_on,101) >= @Start_date
AND CONVERT(CHAR(10),CourseCompleted.Acquired_on,101) <= @End_Date)
AND Employee.Status <> 'Terminated'
AND Course.CourseCode = @Course_Code
AND ((@Business_Unit IS NULL)
OR (Location.loc_name = @Business_Unit))
--Insert Final dataset into temp table
INSERT INTO #FINAL_RESULTS
SELECT
EmployeeInfo.EmployeeName,
EmployeeInfo.EmployeeID,
EmployeeInfo.JobTitle,
EmployeeInfo.EmpStatus,
EmployeeInfo.Location,
EmployeeInfo.CostCenter,
EmployeeInfo.Station_CostCenter,
ISNULL(EmployeeInfo.Region,'Multiple Use'),
ISNULL(CourseInfo.CourseName,(SUBSTRING(@Course,(charindex ('/',@Course) + 2), LEN(@Course)))),
ISNULL(CourseInfo.CourseCode,(RTRIM(LEFT(@Course,(charindex('/',@Course)-2))))),
ISNULL(CourseInfo.Course,@Course),
EmpCourseInfo.CompletionDate,
CASE WHEN EmpCourseInfo.CompletionDate IS NULL THEN 'N' ELSE 'Y' END,
@Super_User
FROM #EMPLOYEES_INFO EmployeeInfo
LEFT OUTER JOIN
#EMPLOYEE_COURSE_INFO EmpCourseInfo
ON EmployeeInfo.EmployeeID = EmpCourseInfo.EmployeeID
LEFT OUTER JOIN
#COURSE_INFO CourseInfo
ON CourseInfo.CourseId = EmpCourseInfo.CourseId
--Return data set to Crystal Reports
SELECT * FROM #FINAL_RESULTS
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO