ClassroomSchedulingView.sql 9.7 KB

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