I have the following data in SQL Server:

[![enter image description here](https://i.stack.imgur.com/bDuQa.png)](https://i.stack.imgur.com/bDuQa.png)

What I need is that for every day by employee (employeeId) I get in the follwing data:

[![enter image description here](https://i.stack.imgur.com/hmemL.png)](https://i.stack.imgur.com/hmemL.png)

`AccessCode` column means `I = PunchIn` and O = `PunchOut` and we have to filter by `lunchtype = 'N'`

So basically the result should return only one row per day and all the punch ins and punch outs in the middle of the first entrance and last exist shouldn't be considered.

> 
> Any clue?
> 
> 
>