答案 0 :(得分:1)
使用datepart(工作日)来获取相对于星期一和星期五的所有记录。
也可以查看SET DATEFIRST功能。
select *
from your_table
where datepart(weekday, Date) = 5
or datepart(weekday, Date) = 1;
答案 1 :(得分:0)
这将列出周五和下周一(+1周)缺席的所有员工ID。我从表格的最小日期到最大日期设置了一个日历周,并且仅在周五和周一进行。然后得到所有在任何一个日期都没有出席的empid。
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