I need list of those employees who are absent one day before and one day after weekend in a week......like if some is absent in Friday and present on Monday should not be included in the list
Asked
Active
Viewed 104 times
-1
-
Picture is included in question. – IbrahimSediq Feb 25 '18 at 14:32
-
What is your query and the error you are getting? – jose_bacoy Feb 25 '18 at 14:34
-
Are you using a Calendar table? – McNets Feb 25 '18 at 14:34
-
When you say absent on the Friday and the Monday, does it need to be *for the same weekend*? I.e. if they are absent one Friday, in the following Monday, but then absent the following Monday, they would not be included. – Alan Feb 25 '18 at 14:40
-
Yes, those people who doesn't have record in friday of one week and Monday of next week. – IbrahimSediq Feb 26 '18 at 10:44
2 Answers
1
Use datepart(weekday, ) to fetch all records relative to monday and friday.
Have a look at SET DATEFIRST function too.
select *
from your_table
where datepart(weekday, Date) = 5
or datepart(weekday, Date) = 1;

McNets
- 10,352
- 3
- 32
- 61
0
This will list all employee id that are absent on a Friday and the following Monday (+1 week). I set-up a calendar week from mininum date to maximum date from the table and get only Friday and Monday. Then get all empid that has no attendance in any of those dates.
with caldte as (
SELECT dateadd(day, rn - 1, t.mindte) as dates,
datepart(weekday, dateadd(day, rn - 1, t.mindte)) as weekday,
datepart(wk, dateadd(day, rn - 1, t.mindte)) as weeknum
FROM (
select row_number() OVER ( order by c.object_id ) AS rn
FROM sys.columns c) rns,
(select min(dte) as mindte, max(dte) as maxdte
from tbl) t
WHERE rn - 1 <= datediff(day, t.mindte, t.maxdte)
and datepart(weekday, dateadd(day, rn - 1, t.mindte)) in (2, 6)
)
select distinct empid
from tbl
where empid not in (
select t.empid
from caldte c, tbl t
where c.dates = t.dte)
order by empid

jose_bacoy
- 12,227
- 1
- 20
- 38