Here is the table I want to display:
tblAttendance table CustomerId Id Attendence Date and tblStudent CustomerId Name
Now I want to search by from to date and want absent date. How can I achieve this?
I tried below code:
ALTER PROCEDURE spExceptDate AS declare @StartDate DATE, @EndDate DATE set @StartDate = '2020-02-15'; set @EndDate = '2020-02-25'; BEGIN SELECT CustomerId,FirstName+' '+LastName,Date,Attendance FROM [dbo].[tblAttendance] att LEFT JOIN [dbo].[tblStudent] st ON att.CustomerId = st.Code EXCEPT SELECT CustomerId,FirstName+' '+LastName,Date,Attendance FROM [dbo].[tblAttendance] att LEFT JOIN [dbo].[tblStudent] st ON att.CustomerId = st.Code where att.Date>='2020-02-15' and att.Date<='2020-02-25' END GO
i want date for which student absend
Advertisement
Answer
Basically what you need is list of possible dates between From
and To
DECLARE @StartDate DATE = '2020-02-15', @EndDate DATE = '2020-02-25' ; --Create a CTE to get all dates between from and to (you should filter holidays and weekends) WITH SchoolCalendar (WorkingDay) AS (SELECT @StartDate UNION ALL SELECT DATEADD(DAY, 1, WorkingDay) FROM SchoolCalendar WHERE WorkingDay< @EndDate ) --Use the CTE to determine the Absense records SELECT st.Code CustomerId, st.FirstName+' '+st.LastName Name,st.WorkingDay Date, COALESCE(Attendance,'A') Attendance FROM (SELECT * from SchoolCalendar, tblStudent) st LEFT JOIN [dbo].[tblAttendance] att ON att.Date = st.WorkingDay AND att.CustomerId = st.Code WHERE st.WorkingDay>=@StartDate and st.WorkingDay<=@EndDate ORDER BY st.Code, st.WorkingDay