从三个表获取datediff

时间:2018-08-22 01:31:39

标签: sql-server datetime datediff

如何根据员工的日程安排以及“首次上班”和“最后一次上班”获得日期差?

下面是我正在使用的查询。

select * from tbl_payroll_schedule where id in (3, 17, 18)

输出:

id  schedule_description    time_in time_out    schedule_status
3   REST DAY            1900-01-01 00:00:00.000 1900-01-01 00:00:00.000 1
17  08:00 AM - 05:00 PM 1900-01-01 08:00:00.000 1900-01-01 17:00:00.000 1
18  08:00 AM - 12:00 PM 1900-01-01 08:00:00.000 1900-01-01 12:00:00.000 1

select mon.schedule_description as Monday,
        tue.schedule_description as Tuesday,
        wed.schedule_description as Wednesday,
        thu.schedule_description as Thursday,
        fri.schedule_description as Friday,
        sat.schedule_description as Saturday,
        sun.schedule_description as Sunday
        from employee_default_schedule ds

LEFT JOIN tbl_payroll_schedule mon on mon.id = ds.mon_sched
LEFT JOIN tbl_payroll_schedule tue on tue.id = ds.tues_sched
LEFT JOIN tbl_payroll_schedule wed on wed.id = ds.wed_sched
LEFT JOIN tbl_payroll_schedule thu on thu.id = ds.thurs_sched
LEFT JOIN tbl_payroll_schedule fri on fri.id = ds.fri_sched
LEFT JOIN tbl_payroll_schedule sat on sat.id = ds.sat_sched
LEFT JOIN tbl_payroll_schedule sun on sun.id = ds.sun_sched

WHERE ds.employee_id = 125

输出: enter image description here

select ot.id,
        ot.employee_id as EID,
        (el.Last_Name + ', ' + el.First_Name) as Name, 
        FORMAT(ot.record_time, 'ddd MM-dd-yyyy HH:mm:ss') as Time,
        (CASE WHEN ot.type = 0 then 'Time In'
             WHEN ot.type = 1 then 'Time Out'
        END) as Status
        from officer_timelogs ot
        left join tbl_Employee_MasterList el on el.ID = ot.employee_id
        where ot.employee_id = (CASE WHEN 125 = 0 THEN ot.employee_id else 125 END)
            and ot.record_time >= CONVERT(datetime, '08/16/2018 00:00:00', 121) and ot.record_time <= CONVERT(datetime, '08/16/2018 23:59:59', 121)

        order by el.Last_Name asc, ot.record_time asc

输出:

id      EID Name                        Time                Status
194664  125 MOQUETE, ABRAHAM JOSEPH Thu 08-16-2018 07:55:57 Time In
194703  125 MOQUETE, ABRAHAM JOSEPH Thu 08-16-2018 12:11:36 Time Out
194705  125 MOQUETE, ABRAHAM JOSEPH Thu 08-16-2018 12:28:12 Time In
194757  125 MOQUETE, ABRAHAM JOSEPH Thu 08-16-2018 20:45:08 Time Out

目前,我可以得到总的工作时间,我想获得“延迟和加班”。

select
    employee_id as EID, 
    FORMAT(date, 'MM-dd-yyyy') as [Date], 
    DATENAME(WEEKDAY, date) as [Day],
    right(concat('00', diff / 4100), 2) + ':' + right(concat('00', diff % 3600 / 60), 2) + ':' + right(concat('00', diff % 60), 2) as TWH
    from (select
            employee_id, 
            [date] = cast(actual_time as date), 
            diff = datediff(ss, min(iif(type = 0, actual_time, null)), max(iif(type = 1, actual_time, null)))
        from
            officer_timelogs
            where employee_id = (CASE WHEN @employee_id = 0 THEN employee_id else @employee_id END)
        group by employee_id, cast(actual_time as date)
        ) t
    where t.date >= @dateFrom and t.date <= @dateTo
    order by date asc

预期输出:

EID Date        Day         TWH        Late    OT
125 08-16-2018  Thursday    11:49:11   

0 个答案:

没有答案