TeacherSchedulingView.sql 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. SELECT distinct ct.CoursesTimeID
  2. ,ct.StartTimes
  3. ,ct.Times
  4. ,ct.TimeDesc
  5. ,w.Weekday
  6. ,w.WeekdayDesc
  7. ,s.SchoolyearID
  8. ,s.Code
  9. ,es.ClassroomID
  10. ,es.EducationSchedulingClassID
  11. ,es.TaskGroupName
  12. ,es.StudentCount
  13. ,es.EducationMissionClassID
  14. ,es.OrderNo
  15. ,es.Name
  16. ,es.TeachingModeID
  17. ,es.EducationMissionID
  18. ,u.CollegeID
  19. ,u.CollegeName
  20. ,u.DepartmentID
  21. ,u.DepartmentName
  22. ,es.CourseCode
  23. ,es.CourseName
  24. ,es.StartWeeklyNum
  25. ,es.EndWeeklyNum
  26. ,es.ClassroomName
  27. ,u.UserID
  28. ,u.StaffName
  29. ,es.TeacherName
  30. ,es.WeekNums
  31. ,es.MinWeekNum
  32. ,es.StopWeekNumStr
  33. FROM (select ct.CoursesTimeID,ct.StartTimes,convert(varchar(10),ct.StartTimes)+'-'+CONVERT(varchar(10),ct.EndTimes) as Times,
  34. REPLICATE('0',2-len(convert(varchar(10),ct.StartHour)))+convert(varchar(10),ct.StartHour)
  35. +':'+REPLICATE('0',2-len(convert(varchar(10),ct.StartMinutes)))+convert(varchar(10),ct.StartMinutes)
  36. +'-'+REPLICATE('0',2-len(convert(varchar(10),ct.EndHour)))+convert(varchar(10),ct.EndHour)
  37. +':'+REPLICATE('0',2-len(convert(varchar(10),ct.EndMinutes)))+convert(varchar(10),ct.EndMinutes) as TimeDesc
  38. from EM_CoursesTime ct
  39. where ct.RecordStatus>0
  40. ) ct
  41. full join (
  42. select 1 as Weekday,'星期一' as WeekdayDesc
  43. union all
  44. select 2 as Weekday,'星期二' as WeekdayDesc
  45. union all
  46. select 3 as Weekday,'星期三' as WeekdayDesc
  47. union all
  48. select 4 as Weekday,'星期四' as WeekdayDesc
  49. union all
  50. select 5 as Weekday,'星期五' as WeekdayDesc
  51. union all
  52. select 6 as Weekday,'星期六' as WeekdayDesc
  53. union all
  54. select 0 as Weekday,'星期日' as WeekdayDesc
  55. ) w on 1 =1
  56. inner join CF_Schoolyear s on 1=1
  57. inner join (
  58. select es.SchoolyearID,c.CollegeID,c.Name as CollegeName,d.DepartmentID,d.Name as DepartmentName,u.UserID,u.Name as StaffName
  59. from Sys_User u
  60. left join CF_Staff s on u.UserID=s.UserID
  61. left join CF_Department d on s.DepartmentID=d.DepartmentID
  62. left join CF_College c on s.CollegeID=c.CollegeID
  63. inner join (
  64. select em.SchoolyearID,escc.UserID
  65. from ES_EducationScheduling es
  66. inner join EM_CoursesTime ct on es.CoursesTimeID=ct.CoursesTimeID and ct.RecordStatus>0
  67. inner join EM_EducationSchedulingClass esc on es.EducationSchedulingClassID=esc.EducationSchedulingClassID
  68. inner join EM_EducationMissionClass emc on esc.EducationMissionClassID=emc.EducationMissionClassID
  69. inner join EM_EducationMission em on emc.EducationMissionID=em.EducationMissionID
  70. inner join ES_EducationSchedulingTeacher escc on es.EducationSchedulingID=escc.EducationSchedulingID
  71. group by em.SchoolyearID,escc.UserID
  72. ) es on u.UserID=es.UserID
  73. ) u on s.SchoolyearID = u.SchoolyearID
  74. left join (
  75. select distinct x.*,wn.MinWeekNum,case when right(wn.WeekNums,1)=',' then substring(wn.WeekNums,0,LEN(wn.WeekNums)) else wn.WeekNums end as WeekNums
  76. from (
  77. SELECT
  78. ES_EducationScheduling.Weekday
  79. ,ES_EducationScheduling.CoursesTimeID
  80. ,ES_EducationScheduling.ClassroomID
  81. ,EM_EducationSchedulingClass.EducationSchedulingClassID
  82. ,EM_EducationSchedulingClass.TaskGroupName
  83. ,ISNULL(esccs.StudentCount,0) AS StudentCount
  84. ,EM_EducationMissionClass.EducationMissionClassID
  85. ,EM_EducationMissionClass.OrderNo
  86. ,EM_EducationMissionClass.Name
  87. ,EM_EducationMissionClass.TeachingModeID
  88. ,EM_EducationMission.EducationMissionID
  89. ,EM_EducationMission.CollegeID
  90. ,EM_EducationMission.SchoolyearID
  91. ,EM_Coursematerial.CourseCode
  92. ,EM_Coursematerial.CourseName
  93. ,EM_EducationMissionClassTeachingSetting.StartWeeklyNum
  94. ,EM_EducationMissionClassTeachingSetting.EndWeeklyNum
  95. ,CF_Classroom.Name AS ClassroomName
  96. ,CF_Classmajor.ClassmajorID
  97. ,CF_Classmajor.Name AS ClassMajorName
  98. ,ES_EducationSchedulingTeacher.UserID
  99. ,mct.TeacherName
  100. ,ess.StopWeekNumStr
  101. FROM
  102. ES_EducationScheduling
  103. INNER JOIN EM_EducationSchedulingClass
  104. ON EM_EducationSchedulingClass.EducationSchedulingClassID = ES_EducationScheduling.EducationSchedulingClassID
  105. LEFT JOIN (
  106. SELECT EducationSchedulingClassID,COUNT(1) AS StudentCount
  107. FROM EM_EducationSchedulingClass_CF_Student
  108. GROUP BY EducationSchedulingClassID
  109. ) esccs ON EM_EducationSchedulingClass.EducationSchedulingClassID=esccs.EducationSchedulingClassID
  110. INNER JOIN EM_EducationMissionClass
  111. ON EM_EducationSchedulingClass.EducationMissionClassID = EM_EducationMissionClass.EducationMissionClassID
  112. INNER JOIN EM_EducationMission
  113. ON EM_EducationMissionClass.EducationMissionID = EM_EducationMission.EducationMissionID
  114. INNER JOIN EM_EducationMissionClassTeachingSetting
  115. ON EM_EducationMissionClass.EducationMissionClassID = EM_EducationMissionClassTeachingSetting.EducationMissionClassID
  116. INNER JOIN EM_Coursematerial
  117. ON EM_EducationMissionClass.CoursematerialID = EM_Coursematerial.CoursematerialID
  118. INNER JOIN CF_Classroom
  119. ON ES_EducationScheduling.ClassroomID = CF_Classroom.ClassroomID
  120. LEFT JOIN EM_EducationMissionClass_CF_Classmajor
  121. ON EM_EducationMissionClass.EducationMissionClassID = EM_EducationMissionClass_CF_Classmajor.EducationMissionClassID
  122. LEFT JOIN CF_Classmajor
  123. ON EM_EducationMissionClass_CF_Classmajor.ClassmajorID = CF_Classmajor.ClassmajorID
  124. INNER JOIN ES_EducationSchedulingTeacher
  125. ON ES_EducationScheduling.EducationSchedulingID = ES_EducationSchedulingTeacher.EducationSchedulingID
  126. LEFT JOIN (select es.EducationSchedulingID, STUFF((select ','+tu.Name
  127. from ES_EducationSchedulingTeacher tescs
  128. inner join ES_EducationScheduling tes on tescs.EducationSchedulingID=tes.EducationSchedulingID
  129. inner join EM_EducationSchedulingClass tesc on tes.EducationSchedulingClassID=tesc.EducationSchedulingClassID
  130. inner join Sys_User tu on tescs.UserID=tu.UserID
  131. where tescs.EducationSchedulingID=es.EducationSchedulingID
  132. order by ISNULL(tescs.TeachingMethod,0),tu.Name
  133. for xml path('')), 1, 1, '') as TeacherName
  134. from ES_EducationSchedulingTeacher escs
  135. inner join ES_EducationScheduling es on escs.EducationSchedulingID=es.EducationSchedulingID
  136. inner join EM_EducationSchedulingClass esc on es.EducationSchedulingClassID=esc.EducationSchedulingClassID
  137. inner join Sys_User u on escs.UserID=u.UserID
  138. group by es.EducationSchedulingID
  139. ) mct ON ES_EducationScheduling.EducationSchedulingID = mct.EducationSchedulingID
  140. LEFT JOIN (
  141. select es.EducationSchedulingID,STUFF((select ','+Convert(varchar(10),ISNULL(tess.WeekNum,0))
  142. from ES_EducationSchedulingStop tess
  143. inner join ES_EducationSchedulingStopTeacher tesst on tess.EducationSchedulingStopID=tesst.EducationSchedulingStopID
  144. inner join Sys_DictionaryItem ttm on ttm.DictionaryCode='EM_TeachingMethod' and ttm.Code='Lecturer' and tesst.TeachingMethod=ttm.Value
  145. inner join EM_EducationMissionClass temc on tess.EducationMissionClassID=temc.EducationMissionClassID
  146. inner join EM_EducationSchedulingClass tesc on temc.EducationMissionClassID=tesc.EducationMissionClassID
  147. inner join ES_EducationScheduling tes on tesc.EducationSchedulingClassID=tes.EducationSchedulingClassID
  148. and tes.Weekday=tess.Weekday and tes.CoursesTimeID=tess.CoursesTimeID and tes.ClassroomID=tess.ClassroomID
  149. where tes.EducationSchedulingID=es.EducationSchedulingID
  150. and tess.RecordStatus=3
  151. order by ISNULL(tess.WeekNum,0)
  152. for xml path('')), 1, 1, '') as StopWeekNumStr
  153. from ES_EducationSchedulingStop ess
  154. inner join ES_EducationSchedulingStopTeacher esst on ess.EducationSchedulingStopID=esst.EducationSchedulingStopID
  155. inner join Sys_DictionaryItem tm on tm.DictionaryCode='EM_TeachingMethod' and tm.Code='Lecturer' and esst.TeachingMethod=tm.Value
  156. inner join EM_EducationMissionClass emc on ess.EducationMissionClassID=emc.EducationMissionClassID
  157. inner join EM_EducationSchedulingClass esc on emc.EducationMissionClassID=esc.EducationMissionClassID
  158. inner join ES_EducationScheduling es on esc.EducationSchedulingClassID=es.EducationSchedulingClassID
  159. and es.Weekday=ess.Weekday and es.CoursesTimeID=ess.CoursesTimeID and es.ClassroomID=ess.ClassroomID
  160. inner join ES_EducationSchedulingWeekNum esw on es.EducationSchedulingID=esw.EducationSchedulingID and ess.WeekNum=esw.WeekNum
  161. where ess.RecordStatus=3
  162. group by es.EducationSchedulingID
  163. ) ess on ES_EducationScheduling.EducationSchedulingID=ess.EducationSchedulingID
  164. ) x
  165. left join (
  166. select x.EducationSchedulingClassID,x.Weekday,x.CoursesTimeID,x.ClassroomID,x.TeacherName,
  167. MIN(x.WeekNum) as MinWeekNum,
  168. STUFF((select convert(varchar(10),esw.WeekNum)+','
  169. from ES_EducationScheduling es
  170. inner join ES_EducationSchedulingWeekNum esw on es.EducationSchedulingID=esw.EducationSchedulingID
  171. group by es.EducationSchedulingID,es.EducationSchedulingClassID,es.Weekday,es.CoursesTimeID,es.ClassroomID,esw.WeekNum
  172. having es.EducationSchedulingClassID=x.EducationSchedulingClassID and es.Weekday=x.Weekday
  173. and es.CoursesTimeID=x.CoursesTimeID and es.ClassroomID=x.ClassroomID
  174. and STUFF((select ','+tu.Name
  175. from ES_EducationSchedulingTeacher tescs
  176. inner join ES_EducationScheduling tes on tescs.EducationSchedulingID=tes.EducationSchedulingID
  177. inner join Sys_User tu on tescs.UserID=tu.UserID
  178. where tescs.EducationSchedulingID=es.EducationSchedulingID
  179. order by ISNULL(tescs.TeachingMethod,0),tu.Name
  180. for xml path('')), 1, 1, '')=x.TeacherName
  181. order by esw.WeekNum
  182. for xml path('')
  183. ), 1, 0, '') as WeekNums
  184. from (
  185. select es.EducationSchedulingID,es.EducationSchedulingClassID,es.Weekday,es.CoursesTimeID,es.ClassroomID,STUFF((select ','+tu.Name
  186. from ES_EducationSchedulingTeacher tescs
  187. inner join ES_EducationScheduling tes on tescs.EducationSchedulingID=tes.EducationSchedulingID
  188. inner join Sys_User tu on tescs.UserID=tu.UserID
  189. where tescs.EducationSchedulingID=es.EducationSchedulingID
  190. order by ISNULL(tescs.TeachingMethod,0),tu.Name
  191. for xml path('')), 1, 1, '') as TeacherName,esw.WeekNum
  192. from ES_EducationScheduling es
  193. inner join ES_EducationSchedulingWeekNum esw on es.EducationSchedulingID=esw.EducationSchedulingID
  194. group by es.EducationSchedulingID,es.EducationSchedulingClassID,es.Weekday,es.CoursesTimeID,es.ClassroomID,esw.WeekNum
  195. ) x
  196. group by x.EducationSchedulingClassID,x.Weekday,x.CoursesTimeID,x.ClassroomID,x.TeacherName
  197. ) wn on x.EducationSchedulingClassID=wn.EducationSchedulingClassID and x.Weekday=wn.Weekday
  198. and x.CoursesTimeID=wn.CoursesTimeID and x.ClassroomID=wn.ClassroomID and x.TeacherName=wn.TeacherName
  199. ) es
  200. on ct.CoursesTimeID=es.CoursesTimeID
  201. and w.Weekday=es.Weekday
  202. and s.SchoolyearID=es.SchoolyearID
  203. and u.UserID=es.UserID
  204. where s.SchoolyearID=@SchoolyearID
  205. and (u.CollegeID=@CollegeID or @CollegeID is null)
  206. and (u.UserID=@UserID or @UserID is null)