Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.71 KB | None | 0 0
  1. employee_id logdatetime tkstype
  2. PH120013 2012-08-25 08:30:00.000 0
  3. PH120013 2012-08-25 13:00:00.000 1
  4. PH120013 2012-08-26 02:30:00.000 0
  5. PH120013 2012-08-27 15:00:00.000 1
  6. PH120013 2012-08-27 17:00:00.000 0
  7. PH120013 2012-08-27 21:00:00.000 1
  8. PH120013 2012-08-28 08:00:00.000 0
  9. PH120013 2012-08-28 19:00:00.000 1
  10. PH120013 2012-08-29 03:30:00.000 0
  11. PH120013 2012-08-29 21:00:00.000 1
  12. PH120013 2012-08-30 05:30:00.000 0
  13. PH120013 2012-08-30 19:00:00.000 1
  14. PH120013 2012-08-31 05:30:00.000 0
  15. PH120013 2012-08-31 20:30:00.000 1
  16. PH120013 2012-09-01 01:00:00.000 1
  17. PH120013 2012-09-01 21:00:00.000 0
  18.  
  19. IN OUT
  20.  
  21. ;With Attendance_CTE As
  22. (
  23.  
  24. SELECT employee_id,CONVERT(VARCHAR(10), logdatetime, 120) logdate,
  25. CONVERT(VARCHAR(12), logdatetime, 114) logtime,logdatetime,tkstype
  26. FROM MSTR_ATTENDANCE WHERE CONVERT(VARCHAR(10), logdatetime, 120) >='2012-08-26'
  27.  
  28. ),
  29. Employee_CTE As
  30. (
  31. SELECT employee_id,
  32. '2012-08-26' + CONVERT(datetime,tks_ofc_in,108) as Official_IN,
  33. '2012-08-26' + CONVERT(datetime,tks_ofc_out,108) as Official_OUT,
  34. hireddate + '00:00:00.000' as HiredDate_DateTime,
  35. '2012-08-26' + ' 00:00:00.000' as TKS_DateTime
  36. FROM vwu_Employee_Contract_Info
  37. ),
  38. Min_Max_Attendance As
  39. (
  40. SELECT t_in.employee_id,
  41. min(isnull(t_in.logdatetime,'1753-01-01 00:00:00.000')) min_in,
  42. max(isnull(t_out.logdatetime,'1753-01-01 00:00:00.000')) max_out
  43. FROM
  44. (SELECT employee_id,logdatetime FROM Mstr_Attendance WHERE tkstype=0) t_in
  45. LEFT JOIN
  46. (SELECT employee_id,logdatetime FROM Mstr_Attendance WHERE tkstype=1) t_out
  47. ON
  48. t_in.employee_id=t_out.employee_id
  49. GROUP BY
  50. t_in.employee_id
  51. )
  52. SELECT Attendance_CTE.Employee_ID,logdatetime,
  53. CASE WHEN TKSType=0 THEN
  54. CASE Min_Max_Attendance.min_in
  55. -- first day
  56. WHEN '1753-01-01 00:00:00.000' THEN
  57. CASE
  58. -- check if current logdatetime is within the hireddate and current date query
  59. WHEN Attendance_CTE.LogDateTime BETWEEN HiredDate_DateTime AND TKS_DateTime
  60. THEN
  61. CASE
  62. WHEN Attendance_CTE.LogDateTime BETWEEN Employee_CTE.Official_In AND
  63. Employee_CTE.Official_Out THEN Attendance_CTE.LogDateTime
  64. END
  65. ELSE
  66. -- no time in?
  67. '1753-01-01 00:00:00.000'
  68. END
  69. ELSE
  70. -- regular days
  71. CASE
  72. -- check whether no out
  73. WHEN Min_Max_Attendance.max_out = '1753-01-01 00:00:00.000' THEN Attendance_CTE.Logdatetime
  74. ELSE
  75. -- but with previous out
  76. CASE
  77. -- check whether logdatetime is not greater than the TKS_DateTime
  78. WHEN Convert(varchar(10),Attendance_CTE.logdatetime,120) !> TKS_DateTime THEN
  79. CASE
  80. -- when logdatetime range on employee official time including late
  81. WHEN Attendance_CTE.logdatetime BETWEEN Employee_CTE.Official_In AND Employee_CTE.Official_Out THEN Attendance_CTE.Logdatetime
  82. -- when logdatetime is less than the max l_out on the records
  83. WHEN Attendance_CTE.LogDateTime <= Min_Max_Attendance.max_out THEN Attendance_CTE.LogDateTime
  84. -- when logdatetime is on range between the min l_in and max l_out on the records
  85. WHEN Attendance_CTE.LogDateTime BETWEEN Min_Max_Attendance.min_in AND Min_Max_Attendance.max_out THEN Attendance_CTE.LogDateTime
  86. -- when logdatetime is on range between current query date to official in
  87. WHEN Attendance_CTE.LogDateTime BETWEEN TKS_DateTime AND Employee_CTE.Official_In THEN Attendance_CTE.LogDateTime
  88. -- when logdatetime is not greater than equal to current query date
  89. WHEN Attendance_CTE.LogDateTime < TKS_DateTime THEN Attendance_CTE.LogDateTime
  90. END
  91. /*ELSE
  92. -- no time in?
  93. '1753-01-01 00:00:00.000'*/
  94. END
  95. END
  96. END
  97. END ActualLogIn,
  98. CASE WHEN TKSType=1 THEN
  99. CASE
  100. -- check whether logdatetime is less than official_out and greater than official_in, undertime
  101. WHEN Attendance_CTE.LogDateTime > Employee_CTE.Official_Out AND Attendance_CTE.LogDateTime !> Employee_CTE.Official_In
  102. THEN Attendance_CTE.LogDateTime
  103. -- logdatetime is greater than official out and not greater than the next log in
  104. WHEN Attendance_CTE.LogDateTime >= Employee_CTE.Official_Out AND Attendance_CTE.LogDateTime !> '2012-08-27 17:00:00.000'
  105. THEN Attendance_CTE.LogDateTime
  106. WHEN Attendance_CTE.LogDateTime !> '2012-08-27 17:00:00.000'
  107. THEN Attendance_CTE.LogDateTime
  108. END
  109. END ActualLogOut
  110. FROM Employee_CTE
  111. LEFT JOIN Attendance_CTE
  112. ON
  113. Employee_CTE.Employee_ID=Attendance_CTE.Employee_ID
  114. LEFT JOIN Min_Max_Attendance
  115. ON
  116. Employee_CTE.Employee_ID=Min_Max_Attendance.Employee_ID
  117. AND
  118. Attendance_CTE.Employee_ID=Min_Max_Attendance.Employee_ID
  119. WHERE
  120. Employee_CTE.Employee_ID IN ('PH120012','PH120013')
  121.  
  122. select * from (
  123. select logi_in.logdatetime as 'IN' , logi_out.logdatetime as 'OUT'
  124. from MSTR_ATTENDANCE logi_in
  125. left outer join MSTR_ATTENDANCE logi_out
  126. on logi_in.employee_id=logi_out.employee_id
  127. and logi_in.tkstype=0
  128. and logi_out.logdatetime =(
  129. select MIN(logdatetime)
  130. from MSTR_ATTENDANCE MSTR
  131. where MSTR.employee_id = logi_out.employee_id
  132. and MSTR.tkstype = 1
  133. and MSTR.logdatetime > logi_in.logdatetime
  134. )
  135. and logi_in.employee_id='PH120013'
  136. and logi_in.logdatetime='2012-08-26 02:30:00.000')t
  137. where OUT is not null
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement