得到最小的时间和最大的时间。 Sql Server中的Fom Attendance记录

时间:2017-02-15 12:58:21

标签: sql-server sql-server-2008 sql-server-2005 sql-server-2012 sql-server-2008-r2

我正在考勤系统,下面是我在sql server数据库中的数据

|EmpCode|Date       |WorkDate   |CheckIn    |CheckOut   |TotalTime
|143    |2017-02-13 |2017-02-13 |20:09:02   |22:38:50   |2.496666000
|143    |2017-02-13 |2017-02-13 |22:59:19   |23:18:15   |0.315555000
|143    |2017-02-13 |2017-02-13 |23:33:47   |05:04:24   |5.510277000
|143    |2017-02-14 |2017-02-13 |02:52:38   |05:12:04   |2.323888000
|143    |2017-02-14 |2017-02-14 |20:09:26   |21:59:27   |1.833611000
|143    |2017-02-14 |2017-02-14 |22:30:10   |22:49:26   |0.321111000
|143    |2017-02-15 |2017-02-14 |05:05:05   |05:08:13   |0.052222000

- 我希望输出像

|EmpCode|wrk        |InTime     |OutTime    |TotalTime
|143    |2017-02-13 |08:09:02 PM|05:12:04 AM|10:38:00
|143    |2017-02-14 |08:09:26 PM|05:08:13 AM|02:12:00

3 个答案:

答案 0 :(得分:2)

假设你想要那些特定格式:

rextester:http://rextester.com/FUW31296

create table t (
     EmpCode int
  ,  Date datetime
  ,  WorkDate datetime
  ,  CheckIn datetime
  ,  CheckOut datetime
  ,  TotalTime decimal(19, 9))

insert into t values
 (143,'2017-02-13','2017-02-13','20:09:02','22:38:50',2.496666000)
,(143,'2017-02-13','2017-02-13','22:59:19','23:18:15',0.315555000)
,(143,'2017-02-13','2017-02-13','23:33:47','05:04:24',5.510277000)
,(143,'2017-02-14','2017-02-13','02:52:38','05:12:04',2.323888000)
,(143,'2017-02-14','2017-02-14','20:09:26','21:59:27',1.833611000)
,(143,'2017-02-14','2017-02-14','22:30:10','22:49:26',0.321111000)
,(143,'2017-02-15','2017-02-14','05:05:05','05:08:13',0.052222000);

查询:

select 
     EmpCode
  ,  wrk       = convert(varchar(10),WorkDate,120)
  ,  InTime    = format(cast(min(Date + CheckIn) as datetime), 'hh:mm:ss tt')
  ,  OutTime   = format(cast(max(Date + CheckOut) as datetime), 'hh:mm:ss tt')
  ,  TotalTime = format(dateadd(minute,sum(TotalTime)*60,0), 'HH:mm:ss')
from t
group by 
    EmpCode
  , WorkDate

结果:

+---------+------------+-------------+-------------+-----------+
| EmpCode |    wrk     |   InTime    |   OutTime   | TotalTime |
+---------+------------+-------------+-------------+-----------+
|     143 | 2017-02-13 | 08:09:02 PM | 05:12:04 AM | 10:38:00  |
|     143 | 2017-02-14 | 08:09:26 PM | 05:08:13 AM | 02:12:00  |
+---------+------------+-------------+-------------+-----------+

答案 1 :(得分:1)

select  *
,       datediff(minute, first_in, last_out) as Totaltime
from    (
        select  emp_reader_id
        ,       min(case when Event_entry.event_entry_name = 'IN' then trnevents.DT end) as Intime
        ,       max(case when Event_entry.event_entry_name = 'OUT' then trnevents.DT end) as Outtime
        ,       cast(min(trnevents.DT) as date) as date
        from    trnevents inner join Event_entry on trnevents.EventCatId=Event_entry.EventCatId
        group by
                emp_reader_id
        ,       cast(trnevents.DT as date)
        ) as SubQueriesMustBeNamed

答案 2 :(得分:0)

select EmpCode, WorkDate as wrk, min(InTime) as InTime, max(OutTime) as OutTime, Sum(TotalTime) as TotalTime
from yourtable
group by EmpCode, WorkDate

将为每位用户每天提供最小的InTime,最差的OutTime以及TotalTime的总yourtable