Click here to Skip to main content
15,887,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm trying to display data from my table "MST_Permissionrequest" witch contain the data (_date, StartTime, EndTime, EmployeeID)
and from my table "MST_Attendance" witch contain the data (_date, EmpID, Times_In, Times_Out)

I need to display the data per date : for example for the date :06-09-2015

Date          First_Time_In             First_Times_Out   Second_TimeIn          
06-09-2015    06-09-2015 12:30:00   06-09-2015 13:30:00   06-09-2015 15:30:00

Second_TimeOut       StartTimePermission   EndTimePermission 
06-09-2015 17:30:00  10:00                 11:00



I tried the code below witch allow me to display the data from "MST_Permissionrequest" but how can display the others data from "MST_Attendance".

Information: if the employee has times_in and times_out in a specific date and he hasn't permission, the startTimePermission and EndTimePermission should take NULL value.

This is my code:


SQL
WITH per As

	(
	SELECT EmployeeID,
	CAST(Emp_PermissionsRequest._Date As date) As PermissionDate,
	StartTime, 
	EndTime,MSt_Permissions.Permissions as permission, 

	 ROW_NUMBER() OVER (PARTITION BY EmployeeID, CAST(Emp_PermissionsRequest._Date As date) ORDER BY StartTime) As RNI,
	 ROW_NUMBER() OVER (PARTITION BY EmployeeID, CAST(Emp_PermissionsRequest._Date As date) ORDER BY EndTime DESC) As RNO ,
	 ROW_NUMBER() OVER (PARTITION BY EmployeeID, CAST(Emp_PermissionsRequest._Date As date) ORDER BY endtime DESC) As PER ,
	 ROW_NUMBER() OVER (PARTITION BY EmpID, CAST(Emp_Attendance._Date As date) ORDER BY times_in desc) As timesin 
	 FROM dbo.Emp_PermissionsRequest 
	 inner join MSt_Permissions on MSt_Permissions.ID=Emp_PermissionsRequest.PermissionID 
	left outer  join Emp_Attendance on  Emp_Attendance.EmpID=Emp_PermissionsRequest.EmployeeID 
	 WHERE Month(Emp_PermissionsRequest._Date) = 6 And  EmployeeID = 35 and PermissionStatus='Approve' )
	 
	  SELECT


	  EmployeeID,   PermissionDate, --Attendancedate, 
	

	  Max(CASE WHEN RNI = 1 THEN StartTime ELSE Null END) As FirstStartTime,  
	   Max(CASE WHEN RNI = 2 THEN StartTime ELSE Null END) As SecondStartTime, 
	    Max(CASE WHEN RNI = 3 THEN StartTime ELSE Null END) As ThirdStartTime, 
		Max(CASE WHEN RNI = 4 THEN StartTime ELSE Null END) As LastStartTime, 
		Max(CASE WHEN RNO = 2 THEN EndTime ELSE Null END) As FirstEndTime, 
		Max(CASE WHEN RNO = 1 THEN EndTime ELSE Null END) As SecondEndTime,
		 Max(CASE WHEN RNO = 3 THEN EndTime ELSE Null END) As ThirdEndTime,  
		 Max(CASE WHEN RNO = 4 THEN EndTime ELSE Null END) As LastEndTime,
		  Max(CASE WHEN PER = 1 THEN permission ELSE Null END) As Permission ,
		
		   Max(CASE WHEN timesin = 1 THEN Times_IN ELSE Null END) As times_in ,

		CONVERT(varchar(3),sum(DATEDIFF(MINUTE, StartTime, EndTime) )/ 60) 
	 +':' +  RIGHT('0' + CONVERT(varchar(2),sum(DATEDIFF(MINUTE, StartTime, EndTime)) % 60),2) as durationpermission
			   FROM per left join Emp_Attendance on Emp_Attendance.EmpID= per.EmployeeID
		
			   GROUP BY EmployeeID ,PermissionDate --,  
Posted
Updated 19-Jun-15 2:57am
v6
Comments
CHill60 19-Jun-15 6:24am    
You are already joining on MST_Permissions, so do a join on MST_Attendance as well. You will probably need to use a LEFT OUTER join given your comment about NULLs
Member 10286520 19-Jun-15 6:52am    
Thanks fo your help, I tried to update my code according to your idea but it gives me wrong value for the startTimePermission and EndTimePermission (repetition
of same value) . please see the first code above.
CHill60 19-Jun-15 7:12am    
Some sample data and an explanation of what you are really trying to do might help.
Member 10286520 19-Jun-15 10:37am    
this is the result i get when i excute the code above in the question:
https://www.facebook.com/photo.php?fbid=1098227810191505&set=pb.100000128904400.-2207520000.1434724272.&type=3&theater

the values of the starttime and endtime are wrong when added a join with table MST_Attendance0.

But when i delete the join i get the right values:

https://www.facebook.com/photo.php?fbid=1098230010191285&set=pb.100000128904400.-2207520000.1434724621.&type=3&theater

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900