0

I have a data in this format

employee_id date-time           activity
-------------------------------------------
23          01-06-2015 08:00    Punch In
23          01-06-2015 15:23    Punch Out
27          01-08-2015 08:12    Punch In
27          01-08-2015 14:13    Punch Out

I want this.

employee_id punch_in_time       punch_out_time
-------------------------------------------------
23          01-06-2015 08:00    01-06-2015 15:23
27          01-08-2015 08:12    01-08-2015 14:13

I want to write a view from the table to create the data format shown above. Can anyone help? Should I use a pivot?

3
  • Is it only one Punch In and one Punch Out per day? Commented Sep 17, 2015 at 23:32
  • yes, only one activity per day Commented Sep 17, 2015 at 23:33
  • possible duplicate of Explanation of self-joins Commented Sep 18, 2015 at 0:45

1 Answer 1

2

You can do this using conditional aggregation:

SELECT
    employee_id,
    punch_in_time   = MIN(CASE WHEN activity = 'Punch In' THEN [date-time] END),
    punch_out_time  = MAX(CASE WHEN activity = 'Punch Out' THEN [date-time] END)
FROM tbl
GROUP BY employee_id, CAST([date-time] AS DATE)
ORDER BY employee_id, punch_in_time
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.