从另一个别名中减去别名?

时间:2013-07-02 02:29:09

标签: sql alias

尝试在这里做一些基本的数学,但不能让这个东西工作......我想根据一组标准比较一个计算值,然后将这个计算值与相同的标准列表进行比较,但是少变一个。

 SELECT Testa-TestB FROM(
    (SELECT count(loanflag) AS Testa FROM Data
      WHERE declinegroup="XYZ"
        AND orginalrating="A"
        AND score="724-747"
        AND mode="Open"
        AND delqdays>"0")

    (SELECT count(loanflag) AS Testb FROM Data
      WHERE declinegroup="XYZ"
        AND orginalrating="A"
        AND score="724-747"
        AND mode="Open"))

我觉得我一直在研究这个问题太久了,我很想念一些简单的事情!

1 个答案:

答案 0 :(得分:0)

在你想要直接减法

SQL Server中的

SELECT 
    (
        SELECT count(loanflag) 
        FROM Data
        WHERE 
            declinegroup="XYZ"
            AND orginalrating="A"
            AND score="724-747"
            AND mode="Open"
            AND delqdays>"0"
    ) - (
        SELECT count(loanflag) 
        FROM Data
        WHERE declinegroup="XYZ"
        AND orginalrating="A"
        AND score="724-747"
        AND mode="Open"
    )
;

或者,您可以先将值分配给变量,然后再将其减去

DECLARE testa int;
DECLARE testb int;

SET testa = (
        SELECT count(loanflag) 
        FROM Data
        WHERE 
            declinegroup="XYZ"
            AND orginalrating="A"
            AND score="724-747"
            AND mode="Open"
            AND delqdays>"0"
    );

SET testb = (
        SELECT count(loanflag) 
        FROM Data
        WHERE declinegroup="XYZ"
        AND orginalrating="A"
        AND score="724-747"
        AND mode="Open"
    );

Select (testa - testb);