Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL减去使用case函数从现有列生成的值_Sql_Sql Server 2008_Subtraction_Select Case - Fatal编程技术网

SQL减去使用case函数从现有列生成的值

SQL减去使用case函数从现有列生成的值,sql,sql-server-2008,subtraction,select-case,Sql,Sql Server 2008,Subtraction,Select Case,我已经根据现有的文本等级列创建了数字的附加列 例如,如果结果为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

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

例如,如果结果为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

出于兴趣,我尝试在结果集中创建不在原始表中的列的术语是什么?但不确定这个词有多普遍,或者是否还有其他词。
;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;