1

我已经根据现有的文本等级列创建了附加的数字列。

例如,如果结果是 C,则结果列 14

Tar  TarVal    TA   TAVal
A    20        A-   19
B    17        B+   18
C    14        B+   18

我想做的是减去两个结果列并使用结果创建一个新列,例如TAVal - TarVal

Tar  TarVal    TA   TAVal    Difference
A    20        A-   19       -1
B    17        B+   18       1
C    14        B+   18       4

这是我到目前为止的代码,但到目前为止我还没有减去每一列的减法:

    select subject.target AS Tar, 
    (case subject.target
    when 'A*' then 23 when 'A*-' then 22 when 'A+' then 21 when 'A' then 20
    when 'A-' then 19 when 'B+' then 18 when 'B' then 17 when 'B-' then 16
    when 'C+' then 15 when 'C' then 14 when 'C-' then 13 when 'D+' then 12
    when 'D' then 11 when 'D-' then 10 when 'E+' then 9 when 'E' then 8
    when 'E-' then 7 when 'F+' then 6 when 'F' then 5 when 'F-' then 4
    when 'G+' then 3 when 'G' then 2 when 'G-' then 1 when 'U' then 0 end) as TarVal,

    subject.result AS TA, 
    (case subject.result 
    when 'A*' then 23 when 'A*-' then 22 when 'A+' then 21 when 'A' then 20
    when 'A-' then 19 when 'B+' then 18 when 'B' then 17 when 'B-' then 16
    when 'C+' then 15 when 'C' then 14 when 'C-' then 13 when 'D+' then 12
    when 'D' then 11 when 'D-' then 10 when 'E+' then 9 when 'E' then 8
    when 'E-' then 7 when 'F+' then 6 when 'F' then 5 when 'F-' then 4 
    when 'G+' then 3 when 'G' then 2 when 'G-' then 1 when 'U' then 0 end) as TAVal

    from subject 
    join student on subject.upn=student.upn 
    where subject.datacollection='March 2013' and student.stuyear=11 and 
    subject.name='English'
    order by student.surname, student.forename
4

1 回答 1

3
;WITH SQ AS (
select
    student.surname, student.forename,
    subject.target AS Tar, 
    (case subject.target
    when 'A*' then 23 when 'A*-' then 22 when 'A+' then 21 when 'A' then 20
    when 'A-' then 19 when 'B+' then 18 when 'B' then 17 when 'B-' then 16
    when 'C+' then 15 when 'C' then 14 when 'C-' then 13 when 'D+' then 12
    when 'D' then 11 when 'D-' then 10 when 'E+' then 9 when 'E' then 8
    when 'E-' then 7 when 'F+' then 6 when 'F' then 5 when 'F-' then 4
    when 'G+' then 3 when 'G' then 2 when 'G-' then 1 when 'U' then 0 end) as TarVal,

    subject.result AS TA, 
    (case subject.result 
    when 'A*' then 23 when 'A*-' then 22 when 'A+' then 21 when 'A' then 20
    when 'A-' then 19 when 'B+' then 18 when 'B' then 17 when 'B-' then 16
    when 'C+' then 15 when 'C' then 14 when 'C-' then 13 when 'D+' then 12
    when 'D' then 11 when 'D-' then 10 when 'E+' then 9 when 'E' then 8
    when 'E-' then 7 when 'F+' then 6 when 'F' then 5 when 'F-' then 4 
    when 'G+' then 3 when 'G' then 2 when 'G-' then 1 when 'U' then 0 end) as TAVal

    from subject 
    join student on subject.upn=student.upn 
    where subject.datacollection='March 2013' and student.stuyear=11 and 
    subject.name='English'
)
SELECT Tar, TarVal, TA, TAVal, TAVal - TarVal Difference
FROM SQ
order by surname, forename;
于 2013-04-03T20:45:57.080 回答