解释:下面是某准考证号的考室分配情况,为方便理解,这里分3个人为一个考室...(真实考室是30人)
后面的这个room的值.我是用(right(exam_number,4)-1)/3+1公式计算出来的.
现在出现一种情况:
一个考场有1-20个考室.并且这些科目在同一时间进行考试...
我应该如何处理room数据.
才能做到让他们在不重坐的情况下.按照顺序准确的分配到理想考室去.
这里的大致思路是:
从下一个科目开始,考室号码=room+上一个科目中的max(room)
注:科目数据由小到大.不连续
数据如下:
---------------------------------------------
exam_number     room
100100010001 1  (01为第1科目)
100100010002 1
100100010003 1
100100010004 2
100100010005 2
100100010006 2
100100010007 3
100100010008 3
100100010009 3
…………
100100020001 1  (02为第2科目)
100100020002 1
100100020003 1
100100020004 2
100100020005 2
100100020006 2
100100020007 3
100100020008 3
100100020009 3
…………
100100060001 1
100100060002 1
100100060003 1
100100060004 2
100100060005 2
100100060006 2
100100060007 3
100100060008 3
100100060009 3
…………
或许是因为我的思路没有打开.
或许是有更好的方法我没有想到.
真诚求教.希望师兄们指导一下.
象这样的数据.
我们应该怎么处理才能达到想要的目的.

解决方案 »

  1.   

    如果一个考场30人,请把3.0改成30.0
    create table examinees(exam_number varchar(16), room int)insert examinees(exam_number) select '100100010001'
    union all select '100100010002' 
    union all select '100100010003' 
    union all select '100100010004' 
    union all select '100100010005' 
    union all select '100100010006' 
    union all select '100100010007' 
    union all select '100100010008' 
    union all select '100100010009' 
    union all select '100100020001' 
    union all select '100100020002' 
    union all select '100100020003' 
    union all select '100100020004'
    union all select '100100020005' 
    union all select '100100020006' 
    union all select '100100020007' 
    union all select '100100020008'
    union all select '100100020009' 
    union all select '100100020010'
    union all select '100100060001' 
    union all select '100100060002' 
    union all select '100100060003' 
    union all select '100100060004' 
    union all select '100100060005'
    union all select '100100060006' 
    union all select '100100060007' 
    union all select '100100060008' 
    union all select '100100060009'update examinees
    set room = ceiling((select count(*) from examinees e where substring(exam_number, 5, 4)<substring(examinees.exam_number, 5, 4))/3.0)
    + ceiling(cast(right(exam_number, 4) as int)/3.0)select * from examinees
    /*
    exam_number      room
    ---------------- -----------
    100100010001     1
    100100010002     1
    100100010003     1
    100100010004     2
    100100010005     2
    100100010006     2
    100100010007     3
    100100010008     3
    100100010009     3
    100100020001     4
    100100020002     4
    100100020003     4
    100100020004     5
    100100020005     5
    100100020006     5
    100100020007     6
    100100020008     6
    100100020009     6
    100100020010     7
    100100060001     8
    100100060002     8
    100100060003     8
    100100060004     9
    100100060005     9
    100100060006     9
    100100060007     10
    100100060008     10
    100100060009     10(28 row(s) affected)
    */drop table examinees
      

  2.   

     牛,楼上的对ms server 2000很懂,我跟你混了。
      

  3.   

    @dobear_0922 
    这条语句还有一点点问题....
    如:create table examinees(exam_number varchar(16), room int)insert examinees(exam_number) select '100100010001'
    union all select '100100010002' 
    union all select '100100010003' 
    union all select '100100010004' union all select '100100020001' 
    union all select '100100020002' 
    union all select '100100020003' 
    union all select '100100020004'
    union all select '100100020005' union all select '100100040001'
    union all select '100100050001'union all select '100100060001' 
    union all select '100100060002' 
    union all select '100100060003' 
    union all select '100100060004' 
    union all select '100100060005' 
    union all select '100100060006' update examinees
    set room = ceiling((select count(*) from examinees e where substring(exam_number, 5, 4)<substring(examinees.exam_number, 5, 4))/3.0)
                + ceiling(cast(right(exam_number, 4) as int)/3.0)select * from examinees
    drop table examinees
    /*
    exam_number     room
    ----------------
    100100010001 1
    100100010002 1
    100100010003 1
    100100010004 2
    100100020001 3
    100100020002 3
    100100020003 3
    100100020004 4
    100100020005 4
    100100040001 4------------(因为是4号科目.应该为单独考室5)
    100100050001 5------------(因为是5号科目.应该为单独考室6)
    100100060001 5---(因为上面错一位下面都会错,还可以发现这里5号考室有4个考生的.)
    100100060002 5
    100100060003 5
    100100060004 6
    100100060005 6
    100100060006 6
    */
      

  4.   

    set nocount on
    create table examinees(exam_number varchar(16), room int)
    insert examinees(exam_number) select '100100010001'
    union all select '100100010002' 
    union all select '100100010003' 
    union all select '100100010004' 
    union all select '100100010005' 
    union all select '100100010006' 
    union all select '100100010007' 
    union all select '100100010008' 
    union all select '100100010009' 
    union all select '100100020001' 
    union all select '100100020002' 
    union all select '100100020003' 
    union all select '100100020004'
    union all select '100100020005' 
    union all select '100100020006' 
    union all select '100100020007' 
    union all select '100100020008'
    union all select '100100020009' 
    union all select '100100020010'
    union all select '100100060001' 
    union all select '100100060002' 
    union all select '100100060003' 
    union all select '100100060004' 
    union all select '100100060005'
    union all select '100100060006' 
    union all select '100100060007' 
    union all select '100100060008' 
    union all select '100100060009'
    go
    update a set room = (select sum((rs+2)/3) from 
            (select rs=count(*) from examinees 
                   where substring(exam_number,7,2)<substring(a.exam_number,7,2) 
                   or (substring(exam_number,7,2)=substring(a.exam_number,7,2) and exam_number<=a.exam_number)
                   group  by substring(exam_number,7,2)) b )
    from examinees a
    select * from examinees
    go
    drop table examinees
    /*
    exam_number      room        
    ---------------- ----------- 
    100100010001               1 
    100100010002               1 
    100100010003               1 
    100100010004               2 
    100100010005               2 
    100100010006               2 
    100100010007               3 
    100100010008               3 
    100100010009               3 
    100100020001               4 
    100100020002               4 
    100100020003               4 
    100100020004               5 
    100100020005               5 
    100100020006               5 
    100100020007               6 
    100100020008               6 
    100100020009               6 
    100100020010               7 
    100100060001               8 
    100100060002               8 
    100100060003               8 
    100100060004               9 
    100100060005               9 
    100100060006               9 
    100100060007              10 
    100100060008              10 
    100100060009              10 
    */