34,590
社区成员
发帖
与我相关
我的任务
分享
select 课程代码 = CourseCode
, 状态0 = (select count(Type) from ExemptStudent where CourseCode = e.CourseCode and Type = 0)
, 状态1 = (select count(Type) from ExemptStudent where CourseCode = e.CourseCode and Type = 1)
, 状态为0的学生ID = stuff((select ',' + StudentID from ExemptStudent where CourseCode = e.CourseCode and Type = 0 for xml path('')),1,1,'')
, 状态为1的学生ID = stuff((select ',' + StudentID from ExemptStudent where CourseCode = e.CourseCode and Type = 1 for xml path('')),1,1,'')
from ExemptStudent e
group by CourseCode
select ClassID,CourseCode,sum(case when Type=0 then 1 else 0 end) as Type0,
sum(case when Type=1 then 1 else 0 end) as Type1,
case when Type=0 then StudentID else null end as StudentID0,
case when Type=1 then StudentID else null end as StudentID1
into #temp from ExemptStudent
group by ClassID,CourseCode,case when Type=0 then StudentID else null end,
case when Type=1 then StudentID else null end
declare @classid varchar(20),@course varchar(20),@student0 varchar(200),@student1 varchar(200)
select @classid='',@course='',@student0='',@student1=''
update #temp set @student0=StudentID0=case when @classid=ClassID and @course=CourseCode then
@student0+','+StudentID0 else StudentID0 end,
@student1=StudentID1=case when @classid=ClassID and @course=CourseCode then
@student1+','+StudentID1 else StudentID1 end,
@classid=ClassID,@course=CourseCode
select ClassID,CourseCode,Type0,Type1,MAX(StudentID0),MAX(StudentID0) from #temp group by ClassID,CourseCode,Type0,Type1
create function f_testS(@CourseCode varchar(20),@Type int)
returns varchar(2000)
as
begin
declare @s varchar(2000)
set @s=''
select @s=@s+','+StudentID
from ExemptStudent
where CourseCode=@CourseCode
and type=@type
set @s=stuff(@s,1,1,'')
return @s
end
select CourseCode 课程代码,
sum(case when Type=0 then 1 else 0 end)状态为0,
sum(case when Type=1 then 1 else 0 end)状态为1,
dbo.f_testS(CourseCode,0 )状态为0的学生ID,
dbo.f_testS(CourseCode,1 )状态为1的学生ID
from ExemptStudent
group by CourseCode
/*
课程代码 状态为0 状态为1 状态为0的学生ID 状态为1的学生ID
-------------------- ----------- ----------- ------------------------------------------------------------ ------------------------------------------------------------
30031001 2 1 2310e6b3-605d-4cd7-8ca0-e4b50d430977,eb05be12-56a5-4dba-883d ea182e26-0af7-4326-b1e5-2a341a93e1d2
30031003 2 1 ea182e26-0af7-4326-b1e5-2a341a93e1d2,eb05be12-56a5-4dba-883d 2310e6b3-605d-4cd7-8ca0-e4b50d430977
(所影响的行数为 2 行)
*/
select ClassID,CourseCode,sum(case when Type=0 then 1 else 0 end) as Type0,
sum(case when Type=1 then 1 else 0 end) as Type1,
case when Type=0 then StudentID else null end as StudentID0,
case when Type=1 then StudentID else null end as StudentID1
from ExemptStudent
group by ClassID,CourseCode,case when Type=0 then StudentID else null end,
case when Type=1 then StudentID else null end