学生成绩表
xh  kcmc   zcj  jd
1    aa     59   
1    bb     58 
1    cc     58.5
1    dd     60
2    aa     60.5
2    bb     61.4
3    cc     61.5
绩点对应表
qsxs    jsxs   jd
0 59.9 0
60 60.9 1
61 61.9 1.1
62 62.9 1.2
63 63.9 1.3
64 64.9 1.4
65 65.9 1.5
66 66.9 1.6
67 67.9 1.7
68 68.9 1.8
69 69.9 1.9
70 70.9 2
71 71.9 2.1
72 72.9 2.2
73 73.9 2.3
74 74.9 2.4
75 75.9 2.5
76 76.9 2.6
77 77.9 2.7
78 78.9 2.8
79 79.9 2.9
80 80.9 3
81 81.9 3.1
82 82.9 3.2
83 83.9 3.3
84 84.9 3.4
85 85.9 3.5
86 86.9 3.6
87 87.9 3.7
88 88.9 3.8
89 89.9 3.9
90 90.9 4
91 91.9 4.1
92 92.9 4.2
93 93.9 4.3
94 94.9 4.4
95 95.9 4.5
96 96.9 4.6
97 97.9 4.7
98 98.9 4.8
99 99.9 4.9
100 100.9 5
当zcj在qsxs和jsxs之间时取对应绩点如下表
xh  kcmc   zcj   jd
1    aa     59    0
1    bb     58    0
1    cc     58.5  0
1    dd     60    1
2    aa     60.5  1
2    bb     61.4  1.1
3    cc     61.5

解决方案 »

  1.   

    select m.xh,  
           m.kcmc,  
           m.zcj,
           jd = (select jd from 绩点对应表 n where m.zcj between n.qsxs and n.jsxs ) 
    from 学生成绩表 mselect m.xh,  
           m.kcmc,  
           m.zcj,
           jd = (select top 1 jd from 绩点对应表 n where m.zcj between n.qsxs and n.jsxs order by jd) 
    from 学生成绩表 m
      

  2.   

    ----------------------------------------------------------------
    -- Author  :fredrickhu(我是小F,向高手学习)
    -- Date    :2009-12-23 11:44:34
    -- Version:
    --      Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86) 
    -- Nov 24 2008 13:01:59 
    -- Copyright (c) 1988-2005 Microsoft Corporation
    -- Developer Edition on Windows NT 5.2 (Build 3790: Service Pack 1)
    --
    ----------------------------------------------------------------
    --> 测试数据:[学生成绩表]
    if object_id('[学生成绩表]') is not null drop table [学生成绩表]
    go 
    create table [学生成绩表]([xh] int,[kcmc] varchar(2),[zcj] numeric(3,1),[jd] sql_variant)
    insert [学生成绩表]
    select 1,'aa',59,null union all
    select 1,'bb',58,null union all
    select 1,'cc',58.5,null union all
    select 1,'dd',60,null union all
    select 2,'aa',60.5,null union all
    select 2,'bb',61.4,null union all
    select 3,'cc',61.5,null
    --> 测试数据:[绩点对应表]
    if object_id('[绩点对应表]') is not null drop table [绩点对应表]
    go 
    create table [绩点对应表]([qsxs] int,[jsxs] numeric(4,1),[jd] numeric(2,1))
    insert [绩点对应表]
    select 0,59.9,0 union all
    select 60,60.9,1 union all
    select 61,61.9,1.1 union all
    select 62,62.9,1.2 union all
    select 63,63.9,1.3 union all
    select 64,64.9,1.4 union all
    select 65,65.9,1.5 union all
    select 66,66.9,1.6 union all
    select 67,67.9,1.7 union all
    select 68,68.9,1.8 union all
    select 69,69.9,1.9 union all
    select 70,70.9,2 union all
    select 71,71.9,2.1 union all
    select 72,72.9,2.2 union all
    select 73,73.9,2.3 union all
    select 74,74.9,2.4 union all
    select 75,75.9,2.5 union all
    select 76,76.9,2.6 union all
    select 77,77.9,2.7 union all
    select 78,78.9,2.8 union all
    select 79,79.9,2.9 union all
    select 80,80.9,3 union all
    select 81,81.9,3.1 union all
    select 82,82.9,3.2 union all
    select 83,83.9,3.3 union all
    select 84,84.9,3.4 union all
    select 85,85.9,3.5 union all
    select 86,86.9,3.6 union all
    select 87,87.9,3.7 union all
    select 88,88.9,3.8 union all
    select 89,89.9,3.9 union all
    select 90,90.9,4 union all
    select 91,91.9,4.1 union all
    select 92,92.9,4.2 union all
    select 93,93.9,4.3 union all
    select 94,94.9,4.4 union all
    select 95,95.9,4.5 union all
    select 96,96.9,4.6 union all
    select 97,97.9,4.7 union all
    select 98,98.9,4.8 union all
    select 99,99.9,4.9 union all
    select 100,100.9,5
    --------------开始查询--------------------------
    select
     a.xh,  
     a.kcmc,  
     a.zcj,
     jd = (select jd from 绩点对应表 b where a.zcj between b.qsxs and b.jsxs ) 
    from
     学生成绩表 a----------------结果----------------------------
    /* xh          kcmc zcj                                     jd
    ----------- ---- --------------------------------------- ---------------------------------------
    1           aa   59.0                                    0.0
    1           bb   58.0                                    0.0
    1           cc   58.5                                    0.0
    1           dd   60.0                                    1.0
    2           aa   60.5                                    1.0
    2           bb   61.4                                    1.1
    3           cc   61.5                                    1.1(7 行受影响)*/
      

  3.   

    select a.*,b.jd
    from 学生成绩表 a inner join 绩点对应表 b on a.zcj between b.qsxs and b.jsxs
      

  4.   

    或者,考试的时候稳妥些select a.*,b.jd
    from 学生成绩表 a , 绩点对应表 b 
    where a.zcj between b.qsxs and b.jsxs