日期:2014-05-18  浏览次数:20455 次

学生补选课问题如何实现
开门见山的说我遇到的问题.开学初,我们学校的新生一经都选好课,选课结果都放到了学生选课表里面了,10月份,又补录了一批新生,如何实现这些补录得的学生选课,我的想法是:从学生选课表中统计出一个行政班的学生上的课,比如说这个班的学生上了7门课,然后呢我让补录到这个班的学生选中这7门课,插入到学生选课表中去.例表如下:
学生表:
学号 姓名 班级  
01001 A 01  
01002 B 01  
01003 C 01  
01004 D 01  
02001 02D 02  
02002 02E 02  
02003 02F 02  
02004 02G 02  
选课表:
编号 学号 姓名 班号 课程号
1 01001 A 01 300435  
2 01001 A 01 10110  
3 01001 A 01 201121  
4 02001 02D 02 40014  
5 02001 02D 02 300435  
6 01002 B 01 300435  
7 01002 B 01 10110  
8 01002 B 01 201121  

通过这两张表我们可以看出,只有两个同学选课了,如何实现把其他同学按班级,选择相应的课程,我想的是用游标+存储过程实现,由于水平有限,没成功呀!求大家帮帮忙了,顺便也练练手呀!谢谢了!哪位大侠实现了联系我呀,我的QQ:41438320,呵呵!


------解决方案--------------------
是不是把数据补齐, 这样好象就搞定了吧?

SQL code
set nocount on

create table studentList
(studentID varchar(10), [name] varchar(10), classID varchar(5))
insert studentList
select '01001', 'A', '01'
union all select '01002', 'B', '01'
union all select '01003', 'C', '01'
union all select '01004', 'D', '01'
union all select '02001', '02D', '02'
union all select '02002', '02E', '02'
union all select '02003', '02F', '02'
union all select '02004', '02G', '02'

create table courseList
(serialNo int identity(1,1), studentID varchar(10), [name] varchar(10), classID varchar(5),
courseID varchar(10))

insert courseList(studentID, [name], classID, courseID)
select '01001', 'A', '01', '300435'
union all select '01001', 'A', '01', '10110'
union all select '01001', 'A', '01', '201121'
union all select '02001', '02D', '02', '40014'
union all select '02001', '02D', '02', '300435'
union all select '01002', 'B', '01', '300435'
union all select '01002', 'B', '01', '10110'
union all select '01002', 'B', '01', '201121'
GO

with courseCTE (classID, courseID)
AS
(
    select distinct classID, courseID
    from courseList
)

INSERT courseList
(studentID, [name], classID, courseID)
select sl.studentID, sl.[name], sl.classID, c.courseID
from studentList sl
JOIN courseCTE c
ON sl.classID = c.classID
Where NOT Exists (select 1 from courseList where studentID = sl.studentID AND courseID = c.courseID)
order by [name]
GO

select * from courseList
GO

drop table studentList
drop table courseList
Go

------解决方案--------------------
要安装Analysis Services的吧