I think there's an error in your sample data, because there is no matching out time for N=11. Anyway, try this:;WITH cte AS ( SELECT N, EmpID, Dates, ROW_NUMBER() OVER (PARTITION BY EmpID ORDER BY Dates) AS Row FROM tblAttendance ), cte2 AS ( SELECT *, CASE WHEN (Row / 2) * 2 = Row THEN 1 ELSE 0 END AS Even FROM cte)SELECT a.EmpID, a.Dates AS [In], b.Dates AS [Out] FROM cte2 aINNER JOIN cte2 b ON a.EmpID = b.empID AND a.Row = b.Row - 1 AND a.Even = 0
------------------------------------------------------------------------------------Any and all code contained within this post comes with a 100% money back guarantee.EDIT: N=11, not 8