How can I get the absence days and the present days of all employees , I would like an output like this:
employeeId DATE Status
1 2021/03/04 Absent
1 2021/03/05 Present
2 2021/03/04 Present
2 2021/03/05 Present
What I can get now only the present days :
select distinct DATEPART(dw,er.AddAt) as dayId,
DATENAME(dw,er.AddAt) as dayname,
DATEPART(DAY,er.AddAt) as monthday,
er.employeeId,firstName as Name
from records er,employee
where er.employeeId=employee.employeeId
Update:
After Testing the solution of @Gordon Linoff , that's what I got:
and I have two problems :
I have duplicate data ( 3 times duplicate )
When I select a other month than February , I got no data.
select Day, WeekdayName, e.employeeid,
(case when r.employeeid is not null then 'present' else 'absent' end)
from Get_Calendar_Date(DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE())-1, 0) ,DATEADD(MONTH, DATEDIFF(MONTH, -1, GETDATE())-1, -1)) c cross join
employee e left join
records r
on c.Day = DATEPART(DAY,r.AddAt) and e.employeeid = r.employeeid
where Month=1