2013-07-02 61 views
0

試圖在這裏做一些基本的數學計算,但不能得到這個東西的工作......我想比較基於一組標準的計數值,然後將這個計數值與相同的標準列表進行比較,但只有一個變化較少。從別名中減去別名?

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")) 

我想我一直在這個工作太久了,想念一些容易的事!

回答

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); 
+0

謝謝他們。減去!這兩個工作 – user2540857