---测试数据---
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([试卷编号] int,[学号] int)
insert [tb]
select 1,1 union all
select 2,1 union all
select 3,1 union all
select 4,5 union all
select 5,4 union all
select 6,2 union all
select 7,1 union all
select 8,4 union all
select 4,3 union all
select 5,2 union all
select 6,5
---查询---
select
试卷编号,
学号,
考试时段=(select count(1)+1 from tb where 学号=t.学号 and 试卷编号<t.试卷编号)
from tb t---结果---
试卷编号 学号 考试时段
----------- ----------- -----------
1 1 1
2 1 2
3 1 3
4 5 1
5 4 1
6 2 2
7 1 4
8 4 2
4 3 1
5 2 1
6 5 2(所影响的行数为 11 行)
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([试卷编号] int,[学号] int)
insert [tb]
select 1,1 union all
select 2,1 union all
select 3,1 union all
select 4,5 union all
select 5,4 union all
select 6,2 union all
select 7,1 union all
select 8,4 union all
select 4,3 union all
select 5,2 union all
select 6,5
---查询---
select
试卷编号,
学号,
考试时段=(select count(1)+1 from tb where 学号=t.学号 and 试卷编号<t.试卷编号)
from tb t---结果---
试卷编号 学号 考试时段
----------- ----------- -----------
1 1 1
2 1 2
3 1 3
4 5 1
5 4 1
6 2 2
7 1 4
8 4 2
4 3 1
5 2 1
6 5 2(所影响的行数为 11 行)
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([试卷编号] int,[学号] int)
insert [tb]
select 1,1 union all
select 2,1 union all
select 3,1 union all
select 4,5 union all
select 5,4 union all
select 6,2 union all
select 7,1 union all
select 8,4 union all
select 4,3 union all
select 5,2 union all
select 6,5
---查询---
SELECT *,
ROW_NUMBER() OVER (PARTITION BY [学号] ORDER BY [试卷编号] desc) AS 考试时段
FROM [tb]
/*试卷编号 学号 考试时段
----------- ----------- --------------------
7 1 1
3 1 2
2 1 3
1 1 4
6 2 1
5 2 2
4 3 1
8 4 1
5 4 2
6 5 1
4 5 2(11 行受影响)
*/
insert into @tb select 1,1 union all
select 2,1 union all
select 3,1 union all
select 4,5 union all
select 5,4 union all
select 6,2 union all
select 7,1 union all
select 8,4 union all
select 4,3 union all
select 5,2 union all
select 6,5
select *,ROW_NUMBER() over(partition by 学号 order by 试卷编号) as 考试时段 from @tb (11 行受影响)
试卷编号 学号 考试时段
----------- ----------- --------------------
1 1 1
2 1 2
3 1 3
7 1 4
5 2 1
6 2 2
4 3 1
5 4 1
8 4 2
4 5 1
6 5 2(11 行受影响)
select
*,
考试时段=(select count(1) from tb where 学号=t.学号 and 试卷编号<=t.试卷编号)
from tb t-->2005
SELECT
*,
考试时段=row_number() over(partition by 学号 order by 试卷编号 desc)
from tb
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([试卷编号] int,[学号] int)
insert [tb]
select 1,1 union all
select 2,1 union all
select 2,5 union all
select 3,1 union all
select 4,5 union all
select 5,4 union all
select 6,2 union all
select 7,1 union all
select 8,4 union all
select 4,3 union all
select 5,2 union all
select 6,5
insert [tb]
select 1,1 union all
select 2,1 union all
select 3,1 union all
select 4,5 union all
select 5,4 union all
select 6,2 union all
select 7,1 union all
select 8,4 union all
select 4,3 union all
select 5,2 union all
select 6,5SELECT *,ROW_NUMBER() OVER (PARTITION BY [学号] ORDER BY [试卷编号] desc) AS 考试时段 FROM [tb]
/*试卷编号 学号 考试时段
----------- ----------- --------------------
7 1 1
3 1 2
2 1 3
1 1 4
6 2 1
5 2 2
4 3 1
8 4 1
5 4 2
6 5 1
4 5 2(11 行受影响)
*/
我要的是同一个“试卷编号”对应的肯定是同一个“考试时段”,同一“考试时段”肯定不能对应有两条同一“学号”的记录
怎么把你那M条课程安排到N个考场里面(N指的是一个考生最多的补考数目!)
SQL2000的解决方案二楼的同志试过了,不行,你试试下面的测试数据就知道了---测试数据---
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([试卷编号] int,[学号] int)
insert [tb]
select 1,1 union all
select 2,1 union all
select 2,5 union all
select 3,1 union all
select 4,5 union all
select 5,4 union all
select 6,2 union all
select 7,1 union all
select 8,4 union all
select 4,3 union all
select 5,2 union all
select 6,5
create table [tb]([试卷编号] int,[学号] int)
insert [tb]
select 1,1 union all
select 2,1 union all
select 3,1 union all
select 4,5 union all
select 5,4 union all
select 6,2 union all
select 7,1 union all
select 8,4 union all
select 4,3 union all
select 5,2 union all
select 6,5select * , 考试时段 = row_number() over(partition by 学号 order by 试卷编号 ) from tb t order by 学号 , 考试时段drop table tb/*
试卷编号 学号 考试时段
----------- ----------- --------------------
1 1 1
2 1 2
3 1 3
7 1 4
5 2 1
6 2 2
4 3 1
5 4 1
8 4 2
4 5 1
6 5 2(11 行受影响)*/create table [tb]([试卷编号] int,[学号] int)
insert [tb]
select 1,1 union all
select 2,1 union all
select 3,1 union all
select 4,5 union all
select 5,4 union all
select 6,2 union all
select 7,1 union all
select 8,4 union all
select 4,3 union all
select 5,2 union all
select 6,5select * , 考试时段 = (select count(1) from tb where 学号 = t.学号 and 试卷编号 < t.试卷编号) + 1 from tb t order by 学号 , 考试时段drop table tb/*
试卷编号 学号 考试时段
----------- ----------- -----------
1 1 1
2 1 2
3 1 3
7 1 4
5 2 1
6 2 2
4 3 1
5 4 1
8 4 2
4 5 1
6 5 2(所影响的行数为 11 行)
*/
我现在的想法是配合使用SQL的循环、判断语句,在插入新列“考试时段”后,再对有冲突的实行update,尽量减少手工判断,不知大家有什么好方法,多多赐教!
看这种情况,现在也不敢奢求最小考试时段,保证不冲突是首要问题。
现在想办法首先通过row_number()保证学号不冲突后,如果出现试卷编号冲突的话,就把该试卷编号对应的考试时段数字往后推,避免修改后再造成学号冲突
试卷编号,
学号,
考试时段=(select count(1)+1 from tb where 学号=t.学号 and 试卷编号<t.试卷编号)
from tb t混分的路过