sql除星期六日根据入职日期计算天数,离职根据离职日期计算天数

如题所述

--建议构建一个日历表,能够体现出工作日和非工作日,这样算的话更准;
--下面是简单剔除双休的SQL方法
--mssql:
select a,
       b,
       COUNT(case
               when d in (1, 7) then
                null
               else
                1
             end)
  from (select a, b, c, DATEPART(dw, DATEADD(DAY, t1.number, a)) d
          from (select a, b, DATEDIFF(day, a, b) as c
                  from (select '2012-01-01' as a, '2012-12-31' as b) t) t,
               master .. spt_values t1
         where t1.type = 'P'
           and t1.number < c) t2
 group by a, b;
 --oracle:
 select a,
       b,
       count(case
               when d in (1, 7) then
                null
               else
                1
             end)
  from (select a, b, a + level - 1 as c, to_char(a + level - 1, 'd') d
          from (select to_date('2012-01-01', 'yyyy-mm-dd') a,
                       to_date('2012-12-31', 'yyyy-mm-dd') b
                  from dual)
        connect by level <= b - a)
 group by a, b追问

追答

这个是啥? 算当月应出勤天数?这个表里应该有字段能够判断入职时间和离职时间的吧

温馨提示:答案为网友推荐,仅供参考