ok. then isnt it a matter of just matching record with null timein against previous null timeout record? something like;With CTEAS(SELECT t.EmpNo,convert(datetime,convert(char(11),t1.DateLog)+PrevTimeIn) AS DateIn,convert(datetime,convert(char(11),t2.DateLog)+TimeOut) AS DateOutFROM table tCROSS APPLY (SELECT TOP 1 DateLog,TimeIn AS PrevTimeIn FROM table WHERE EMpNo = t.EmpNo AND DateLog < t.DateLog AND TimeOut IS NULL ORDER BY DateLog DESC,TimeIn DESC )t1 WHERE t.TimeIn IS NULLUNION ALLSELECT EmpNo,convert(datetime,convert(char(11),DateLog) + TimeIn) AS DateIn,convert(datetime,convert(char(11),DateLog)+TimeOut) AS DateOutFROM YourTableWHERE TimeIn > ''AND TimeOut > '')SELECT EmpNo,DateIn,DateOutFROM CTE
I hope your timein,timeout fields are varchar and DateLog is date datatypeApply min max logic over final select to get your required output------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/