Greetings experts,
First, THANK YOU SqlTeam forum developers.
I was here trying to post a thread around after 4PM Eastern time and my browser froze.
I was so upset after waiting for more than one hour for the browser to become active again that I shut down my laptop and went home thinking I had lost all my typings and original thought.
To my sweet surprise, you guys have auto save. That saved me. I have not seen another forum with this incredible feature.
Now on to my problem.
Please forgive me for coming back with this issue.
Recently, ScottPletcher helped me with this issue (Thanks again, Scott) and it works as he coded it.
However, I am still not getting the desired result.
Just to recap, the code displays records for three columns, thisYear, pastYear, pastYears.
All records entered by a particular employee (empID) for the current year displays under the column thisYear.
Records entered the year before displays under pastYear column and all records entered years after last year display under pastYears column. This works as intended.
However, the issue we are currently facing is that it displays all the records (thisYear, pastYear and pastYears).
My question is whether it is possible to display based on latest year?
For instance, if database is queried and the records found are for the thisy year, display those records for thisYear and ignore the rest of the records that are for either past year or past years.
If no record is found for current year but found for last year, display only records found for pastYear and ignore the rest.
Finally, if no records found for current year or past year but found for past years, display only those records and nothing else.
If no records found at all, then no records exist.
Is this possible?
Here is the latest help from ScottPletcher.
Many, many thanks in advance for your assistance.
I have not been to think up a solution.
ALTER PROCEDURE [dbo].[ValidateDates]
@empID varchar(50)
AS
BEGIN
SET NOCOUNT OFF;
SELECT distinct employeeName, email, emptitle, EmpID,d.dateCreated,
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
WHERE e.EmpID=@empID
ORDER By d.dateCreated DESC
END