Greetings again experts.
The code below returns results based or current year, previous year or years after last year.
Thanks to Scott Pletcher for his contributions to making my original code work.
Here is that code:
ALTER PROCEDURE [dbo].[uspGetRecs]
@empID nvarchar(50)
AS
begin
SELECT e.employeeName, e.email, e.emptitle, e.EmpID,s.sourcename, s.sourceaddress,d.dateCreated,sp.spousename, sp.spouseaddress,
r.reimbursementName,r.reimbursementAddress, h.HonorariaName,h.HonorariaAddress,
h.HonorariaName as honoraria,h.HonorariaAddress,h.HonorariaIncome,g.giftName,g.giftAddress,dv.dividentName,dv.dividentAddress,
o.orgName,o.orgAddress,cr.creditorName, cr.creditorAddress,
CASE WHEN d.dateCreated < prev_yr_jan01 THEN 1 ELSE 0 END AS pastYears,
CASE WHEN d.dateCreated >= prev_yr_jan01 AND d.dateCreated < curr_yr_jan01
THEN 1 ELSE 0 END as previousYear,
CASE WHEN d.dateCreated >= curr_yr_jan01 THEN 1 ELSE 0 END as thisYear
FROM Employees e
CROSS APPLY (
SELECT DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()), 0) AS curr_yr_jan01,
DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()) - 1, 0) AS prev_yr_jan01
) AS yr_start_dates
INNER JOIN SourceDetails s ON e.EmployeeID = s.EmployeeID
INNER JOIN SpouseDetails sp ON e.EmployeeID = sp.employeeID
INNER JOIN DividentDetails dv ON e.EmployeeID = dv.EmployeeID
INNER JOIN ReimbursementDetails r ON e.EmployeeID = r.employeeID
INNER JOIN Honoraria h ON e.EmployeeID = h .EmployeeID
INNER JOIN GiftDetails g ON e.EmployeeID = g.employeeID
INNER JOIN dateDetails d ON e.EmployeeID = d.employeeID
INNER JOIN org o ON e.employeeID = o.employeeID
INNER JOIN creditorDetails cr ON e.employeeID = cr.employeeID
WHERE e.EmpID=@empID
ORDER By d.dateCreated DESC
end
Our management determined that it needs to be simplified.
Right now, the new requirement is to query the database and display records (if it exists) based on the latest date the record is created.
This form is completed every year and date the form is created called dateCreated is saved on the database.
For instance, if an employee completed the form in 2020 and 2021, when the employee attempts to complete the form for the year 2022, the database is queried and in this example, we would like the employee records from the latest date of 2021 should be displayed.
Any ideas how to modify this code to just pull records from the database based on the latest date the form was last completed by an employee?
Many thanks in advance for your assistance.