1

I am working on attendance system, and below is my data in sql server database

|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

-- I want the output like

|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
Chanukya
  • 5,833
  • 1
  • 22
  • 36

3 Answers3

2

Assuming you want those specific formats for times:

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);

query:

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

results:

+---------+------------+-------------+-------------+-----------+
| 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  |
+---------+------------+-------------+-------------+-----------+
SqlZim
  • 37,248
  • 6
  • 41
  • 59
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
0
select EmpCode, WorkDate as wrk, min(InTime) as InTime, max(OutTime) as OutTime, Sum(TotalTime) as TotalTime
from yourtable
group by EmpCode, WorkDate

will give you the smallest InTime, the bigest OutTime and the total TotalTime from yourtable per user per day.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
  • Don't think this handles the 4th line correctly as the "Min" checkin would be 02:52:38 and total time is not in the right format. – xQbert Feb 15 '17 at 13:27