Question

I have created addition columns of numbers based on existing columns of text grades.

For example where a result is C there is a resulting column 14

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

What I would like to do is subtract the two resulting columns and create a new column with the results eg TAVal - TarVal:

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

Here is the code I have so far, but the subtraction of each column has so far eluded me:

    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
Était-ce utile?

La solution

;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;
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top