Click here to Skip to main content
15,894,106 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi All

Daily punching of an employee
Type            Date                    Punch time
INPUNCH	        2015-01-02 00:00:00.000	1900-01-01 10:55:40.000
OUTPUNCH	2015-01-02 00:00:00.000	1900-01-01 14:35:36.000
INPUNCH	        2015-01-02 00:00:00.000	1900-01-01 15:15:10.000
OUTPUNCH	2015-01-02 00:00:00.000	1900-01-01 21:42:30.000
.
.
.

How to findout intime ie((OUTPUNCH1-INPUNCH1)+(OUTPUNCH2-INPUNCH2)..+(OUPUNCHN-INPUNCHN)
Outtime ie((INPUNCH2-OUTPUNCH1)+(INPUNCH3-OUTPUNCH2)..+(INPUNCHN-OUTPUNCHN-1)

How to findout total intime and out time ??
Posted
Updated 11-Feb-15 8:04am
v2
Comments
anki.mathur 11-Feb-15 6:58am    
Do you have any Id column or any other unique column in this table?
Zoltán Zörgő 11-Feb-15 14:08pm    
Really? Date and time in two different datetime fields???
Zoltán Zörgő 11-Feb-15 14:18pm    
Which RDBMS do you use?
John C Rayan 12-Feb-15 6:12am    
with cte_inpunch(intime, rowid)
as
select dateadd(nanosecond, datepart(nanosecond,PunchTime),
dateadd(second, datepart(second,PunchTime),
dateadd(minute,datepart(minute,PunchTime),
dateadd(hour,datepart(hour , PunchTime),date)
)
)
) as in_datetime,
Row_Number() Over (Partition By PunchTime order By PunchTime) Rowid
from tblTimeRecords

where type = 'INPUNCH'
;
with cte_outpunch(outtime, rowid)
as
select dateadd(nanosecond, datepart(nanosecond,PunchTime),
dateadd(second, datepart(second,PunchTime),
dateadd(minute,datepart(minute,PunchTime),
dateadd(hour,datepart(hour , PunchTime),date)
)
)
) as out_datetime,
Row_Number() Over (Partition By PunchTime order By PunchTime) Rowid
from tblTimeRecords
where type = 'OUTPUNCH'

select sum(out.outtime - in.intime) as total_in_time , sum(in.intime - out.outtime) as total_out_time
from cte_intime in , cte_outtime out
where in.rowid = out.rowid

-- please note this is not tested code. this is to give you an idea
John C Rayan 12-Feb-15 6:22am    
Try this and please note that it is not tested as I am away from my computer. This is to give you an idea for going about solving your problem.

with cte_inpunch(intime, rowid)
as
select dateadd(nanosecond, datepart(nanosecond,PunchTime),
dateadd(second, datepart(second,PunchTime),
dateadd(minute,datepart(minute,PunchTime),
dateadd(hour,datepart(hour , PunchTime),date)
)
)
) as in_datetime,
Row_Number() Over (Partition By PunchTime order By PunchTime) Rowid
from tblTimeRecords

where type = 'INPUNCH'
;
with cte_outpunch(outtime, rowid)
as
select dateadd(nanosecond, datepart(nanosecond,PunchTime),
dateadd(second, datepart(second,PunchTime),
dateadd(minute,datepart(minute,PunchTime),
dateadd(hour,datepart(hour , PunchTime),date)
)
)
) as out_datetime,
Row_Number() Over (Partition By PunchTime order By PunchTime) Rowid
from tblTimeRecords
where type = 'OUTPUNCH'

select sum(out.outtime - in.intime) as total_in_time , sum(in.intime - out.outtime) as total_out_time
from cte_intime in , cte_outtime out
where in.rowid = out.rowid

1 solution

Try this and please note that it is not tested as I am away from my computer. This is to give you an idea for going about solving your problem.

with cte_inpunch(intime, rowid)
as
select dateadd(nanosecond, datepart(nanosecond,PunchTime), 
                          dateadd(second, datepart(second,PunchTime),
                                          dateadd(minute,datepart(minute,PunchTime),
                                                         dateadd(hour,datepart(hour , PunchTime),date)
                                          )
                          )
       ) as in_datetime,
       Row_Number() Over (Partition By PunchTime order By PunchTime) Rowid         
from tblTimeRecords

where type = 'INPUNCH'
;
with cte_outpunch(outtime, rowid)
as
select dateadd(nanosecond, datepart(nanosecond,PunchTime), 
                          dateadd(second, datepart(second,PunchTime),
                                          dateadd(minute,datepart(minute,PunchTime),
                                                         dateadd(hour,datepart(hour , PunchTime),date)
                                          )
                          )
       ) as out_datetime,
       Row_Number() Over (Partition By PunchTime order By PunchTime) Rowid         
from tblTimeRecords
where type = 'OUTPUNCH'

select sum(out.outtime - in.intime) as total_in_time , sum(in.intime - out.outtime) as total_out_time
from cte_intime in , cte_outtime out
where in.rowid = out.rowid
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900