Skip to content
Advertisement

List No of minutes worked by an employee using self join

I have a SQL Server table emp_swipe_rec which has only 6 columns. I want to know how much time (in minutes) both employees A and B worked for today (last 24 hours). Employee A already swiped out for the day. Employee B is still working. We don’t store two different columns for a swipe in/swipe out. We use swipe_type (I – In/ O- Out) to determine the employee is still working or swipe out.

I need the two outputs from the query. Total Working Minutes of each Employee who swiped in last 24 hours. (Both swipe in/swipe out employees for the past 24 hours) List of employees who are working right now along with their total minutes of work till they swipe in. (Only swipe in and not swipe out for past 24 hours)

Employee_ID Employee_Name Swipe_Type Swipe_In_Out_Time Department Emp_Swipe_Rec_PK
1 A I 2021-03-07 08:00:00.000 MARKETING 1
2 B I 2021-03-07 08:00:00.000 SALES 2
1 A O 2021-03-07 15:00:00.000 MARKETING 3

I tried the below query but it says:

invalid column name for starttime and endtime param inside the DATEDIFF row.

I don’t know what I am doing wrong.

Advertisement

Answer

I think this satisfies your first requirement “Total Working Minutes of each Employee who swiped in last 24 hours”

This returns one record for employee 1 as that is the only employee who has a sign out (in August 2021!?)

As this uses a window function, you will need to be using SQL Server 2012 or above, you have tagged both 2008 and 2012

Also, why is your table not normalized? Do you not have an employees table? EmployeeName and Department are redundant

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement