ClassmajorWithSchedule.sql 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. ,c.CollegeID
  19. ,c.CollegeName
  20. ,es.CourseCode
  21. ,es.CourseName
  22. ,es.StartWeeklyNum
  23. ,es.EndWeeklyNum
  24. ,es.ClassroomName
  25. ,c.ClassmajorID
  26. ,c.ClassMajorName
  27. ,c.GrademajorID
  28. ,c.GrademajorName
  29. ,c.Years
  30. ,es.DepartmentID
  31. ,es.TeacherName
  32. ,es.WeekNums
  33. ,es.MinWeekNum
  34. ,es.StopWeekNumStr
  35. FROM (select ct.CoursesTimeID,ct.StartTimes,convert(varchar(10),ct.StartTimes)+'-'+CONVERT(varchar(10),ct.EndTimes) as Times,
  36. REPLICATE('0',2-len(convert(varchar(10),ct.StartHour)))+convert(varchar(10),ct.StartHour)
  37. +':'+REPLICATE('0',2-len(convert(varchar(10),ct.StartMinutes)))+convert(varchar(10),ct.StartMinutes)
  38. +'-'+REPLICATE('0',2-len(convert(varchar(10),ct.EndHour)))+convert(varchar(10),ct.EndHour)
  39. +':'+REPLICATE('0',2-len(convert(varchar(10),ct.EndMinutes)))+convert(varchar(10),ct.EndMinutes) as TimeDesc
  40. from EM_CoursesTime ct
  41. where ct.RecordStatus>0
  42. ) ct
  43. full join (
  44. select 1 as Weekday,'星期一' as WeekdayDesc
  45. union all
  46. select 2 as Weekday,'星期二' as WeekdayDesc
  47. union all
  48. select 3 as Weekday,'星期三' as WeekdayDesc
  49. union all
  50. select 4 as Weekday,'星期四' as WeekdayDesc
  51. union all
  52. select 5 as Weekday,'星期五' as WeekdayDesc
  53. union all
  54. select 6 as Weekday,'星期六' as WeekdayDesc
  55. union all
  56. select 0 as Weekday,'星期日' as WeekdayDesc
  57. ) w on 1 =1
  58. inner join CF_Schoolyear s on 1=1
  59. inner join (
  60. select es.SchoolyearID,cg.CollegeID,cg.Name as CollegeName,c.ClassmajorID,c.Name as ClassMajorName,g.GrademajorID,g.Name as GrademajorName,g.GradeID AS Years
  61. from CF_Classmajor c
  62. left join CF_Grademajor g on c.GrademajorID=g.GrademajorID
  63. left join CF_Facultymajor f on g.FacultymajorID=f.FacultymajorID
  64. left join CF_College cg on f.CollegeID=cg.CollegeID
  65. inner join (
  66. select em.SchoolyearID,emcc.ClassmajorID
  67. from ES_EducationScheduling es
  68. inner join EM_CoursesTime ct on es.CoursesTimeID=ct.CoursesTimeID and ct.RecordStatus>0
  69. inner join EM_EducationSchedulingClass esc on es.EducationSchedulingClassID=esc.EducationSchedulingClassID
  70. inner join EM_EducationMissionClass emc on esc.EducationMissionClassID=emc.EducationMissionClassID
  71. inner join EM_EducationMission em on emc.EducationMissionID=em.EducationMissionID
  72. inner join EM_EducationMissionClass_CF_Classmajor emcc on emc.EducationMissionClassID=emcc.EducationMissionClassID
  73. group by em.SchoolyearID,emcc.ClassmajorID
  74. ) es on c.ClassmajorID=es.ClassmajorID
  75. ) c on s.SchoolyearID=c.SchoolyearID
  76. left join (
  77. 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
  78. from (
  79. SELECT
  80. ES_EducationScheduling.Weekday
  81. ,ES_EducationScheduling.CoursesTimeID
  82. ,ES_EducationScheduling.ClassroomID
  83. ,EM_EducationSchedulingClass.EducationSchedulingClassID
  84. ,EM_EducationSchedulingClass.TaskGroupName
  85. ,ISNULL(esccs.StudentCount,0) AS StudentCount
  86. ,EM_EducationMissionClass.EducationMissionClassID
  87. ,EM_EducationMissionClass.OrderNo
  88. ,EM_EducationMissionClass.Name
  89. ,EM_EducationMissionClass.TeachingModeID
  90. ,EM_EducationMission.EducationMissionID
  91. ,EM_EducationMission.CollegeID
  92. ,EM_EducationMission.SchoolyearID
  93. ,EM_Coursematerial.CourseCode
  94. ,EM_Coursematerial.CourseName
  95. ,EM_EducationMissionClassTeachingSetting.StartWeeklyNum
  96. ,EM_EducationMissionClassTeachingSetting.EndWeeklyNum
  97. ,CF_Classroom.Name AS ClassroomName
  98. ,CF_Classmajor.ClassmajorID
  99. ,CF_Classmajor.Name AS ClassMajorName
  100. ,EM_EducationMission.DepartmentID
  101. ,mct.TeacherName
  102. ,ess.StopWeekNumStr
  103. FROM
  104. ES_EducationScheduling
  105. INNER JOIN EM_EducationSchedulingClass
  106. ON EM_EducationSchedulingClass.EducationSchedulingClassID = ES_EducationScheduling.EducationSchedulingClassID
  107. LEFT JOIN (
  108. SELECT EducationSchedulingClassID,COUNT(1) AS StudentCount
  109. FROM EM_EducationSchedulingClass_CF_Student
  110. GROUP BY EducationSchedulingClassID
  111. ) esccs ON EM_EducationSchedulingClass.EducationSchedulingClassID=esccs.EducationSchedulingClassID
  112. INNER JOIN EM_EducationMissionClass
  113. ON EM_EducationSchedulingClass.EducationMissionClassID = EM_EducationMissionClass.EducationMissionClassID
  114. INNER JOIN EM_EducationMission
  115. ON EM_EducationMissionClass.EducationMissionID = EM_EducationMission.EducationMissionID
  116. INNER JOIN EM_EducationMissionClassTeachingSetting
  117. ON EM_EducationMissionClass.EducationMissionClassID = EM_EducationMissionClassTeachingSetting.EducationMissionClassID
  118. INNER JOIN EM_Coursematerial
  119. ON EM_EducationMissionClass.CoursematerialID = EM_Coursematerial.CoursematerialID
  120. INNER JOIN CF_Classroom
  121. ON ES_EducationScheduling.ClassroomID = CF_Classroom.ClassroomID
  122. INNER JOIN EM_EducationMissionClass_CF_Classmajor
  123. ON EM_EducationMissionClass.EducationMissionClassID = EM_EducationMissionClass_CF_Classmajor.EducationMissionClassID
  124. INNER JOIN CF_Classmajor
  125. ON EM_EducationMissionClass_CF_Classmajor.ClassmajorID = CF_Classmajor.ClassmajorID
  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 c.ClassmajorID=es.ClassmajorID
  204. where s.SchoolyearID=@SchoolyearID
  205. and (@CollegeID is null or c.CollegeID=@CollegeID)
  206. and (@ClassmajorID is null or c.ClassmajorID=@ClassmajorID)
  207. AND (@Years IS NULL OR c.Years=@Years)