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