两个SQL查询结果的和与差

时间:2013-11-20 10:30:45

标签: sql

(SELECT ID_OF, Col, BNT,    
        SUM(size1) As size1, 
        SUM(size2) As size2,
        SUM(size3) as size3,
        SUM(size4) as size4,
        SUM(size5) as size5,
        SUM(size6)as size6,
        SUM(size7) as size7,
        SUM(size8) as size8,
        SUM(size9) as size9,
        SUM(size10) as size10,
        SUM(Total) as Total,ref 

FROM tblTailleOFALL 
GROUP BY ID_OF, Col, BNT, ref) 

(SELECT ID_OF, Col, BNT,    
        SUM(size1) As size1, 
        SUM(size2) As size2,
        SUM(size3) as size3,
        SUM(size4) as size4,
        SUM(size5) as size5,
        SUM(size6)as size6,
        SUM(size7) as size7,
        SUM(size8) as size8,
        SUM(size9) as size9,
        SUM(size10) as size10,
        SUM(Total) as Total,ref 

FROM tblTailleALL 
GROUP BY ID_OF, Col, BNT, ref)

我在SQL Server中执行了此查询并获得此结果

   Id_OF   Col   BNT   size1   size2   size3   size4   size5   size6   size7   size8   size9   size10   Total   ref
---------
    37623  738   A       60      60      60     30      30      0       0        0        0     0        240      131380  

而且这个:

   Id_OF   Col   BNT   size1   size2   size3   size4   size5   size6   size7   size8   size9   size10   Total   ref
---------
    37623  738   A       60      60      60     30      28      0       0        0        0     0        238     131380  

如何在查询中减去这两个结果!我应该得到这个结果

   Id_OF   Col   BNT   size1   size2   size3   size4   size5   size6   size7   size8   size9   size10   Total   ref
---------
    37623  738   A       0      0        0       0       2       0       0        0        0       0         2     131380  

谢谢堆

5 个答案:

答案 0 :(得分:1)

存在许多方法,具体取决于您的实际逻辑,但对于您提出的问题,我只想使用公共表表达式,子查询或临时表将它们连接在一起,然后从相应的大小列中减去size列

这样的事情:

;WITH T1 AS 
(
    SELECT ID_OF, Col, BNT,    
        SUM(size1) As size1, 
        SUM(size2) As size2,
        SUM(size3) as size3,
        SUM(size4) as size4,
        SUM(size5) as size5,
        SUM(size6)as size6,
        SUM(size7) as size7,
        SUM(size8) as size8,
        SUM(size9) as size9,
        SUM(size10) as size10,
        SUM(Total) as Total,ref 

FROM tblTailleOFALL 
GROUP BY ID_OF, Col, BNT, ref) , 
T2 AS 
(SELECT ID_OF, Col, BNT,    
        SUM(size1) As size1, 
        SUM(size2) As size2,
        SUM(size3) as size3,
        SUM(size4) as size4,
        SUM(size5) as size5,
        SUM(size6)as size6,
        SUM(size7) as size7,
        SUM(size8) as size8,
        SUM(size9) as size9,
        SUM(size10) as size10,
        SUM(Total) as Total,ref 

FROM tblTailleALL 
GROUP BY ID_OF, Col, BNT, ref)
SELECT 
T1.size1 - t2.size1
 .....
 .....
 .....
FROM T1 
INNER JOIN T2 ON T1.ID_OF = T2.ID_OF 

如果外部联接在两个查询之间不匹配,则使用外部联接 (例子只是为了说明)

但正如所说的那样 - 存在这样做的多种方式。

你也可以做一个查询 - 我怀疑 - 并在SUM和GROUP BY之前减去它。

答案 1 :(得分:1)

这看起来很熟悉...... 但是这个怎么样?

 SELECT a.ID_OF, a.Col, a.BNT,    
    SUM(a.size1) - SUM(b.size1) As size1, 
    SUM(a.size2) - SUM(b.size2) As size2,
    SUM(a.size3) - SUM(b.size3) As size3,
    SUM(a.size4) - SUM(b.size4) As size4,
    SUM(a.size5) - SUM(b.size5) As size5,
    SUM(a.size6) - SUM(b.size6) As size6,
    SUM(a.size7) - SUM(b.size7) As size7,
    SUM(a.size8) - SUM(b.size8) As size8,
    SUM(a.size9) - SUM(b.size9) As size9,
    SUM(a.size10) - SUM(b.size10) As size10,
    SUM(a.total) - SUM(b.total) As total,
    a.ref
    FROM tblTailleOFALL a 
    JOIN  tblTailleALL b
      ON a.ID_OF = b.ID_OF
     AND a.Col = b.Col
     AND a.BNT = b.BNT
GROUP BY a.ID_OF, a.Col, a.BNT, a.ref 

答案 2 :(得分:1)

您可以加入表并减去值:

SELECT coalesce(a.ID_OF ,b.id) as id
     , coalesce(a.Col, b.Col)  as Col
     , coalesce(a.BNT, b.BNT)  as BNT
     , SUM(a.size1 -b.size1 )  As size1
     , SUM(a.size2 -b.size2 )  As size2
     , SUM(a.size3 -b.size3 )  as size3
     , SUM(a.size4 -b.size4 )  as size4
     , SUM(a.size5 -b.size5 )  as size5
     , SUM(a.size6 -b.size6 )  as size6
     , SUM(a.size7 -b.size7 )  as size7
     , SUM(a.size8 -b.size8 )  as size8
     , SUM(a.size9 -b.size9 )  as size9
     , SUM(a.size10-b.size10)  as size10
     , SUM(a.Total -b.Total )  as Total
     , coalesce (a.ref, b.ref) as ref
FROM tblTailleOFALL a
full outer join
     tblTailleALL b
on (    a.id = b.id
    and a.Col = b.Col
    and a.BNT = b.BNT
    and a.ref = b.ref)
group by 
       coalesce(a.ID_OF ,b.id)
     , coalesce(a.Col, b.Col)
     , coalesce(a.BNT, b.BNT)
     , coalesce (a.ref, b.ref)

答案 3 :(得分:0)

只需通过ID_OF加入这两个查询并减去外部查询中的每一列:

SELECT a.ID_OF,
  a.Col,
  a.BNT,
  a.size1 - b.size1 AS size1,
  a.size2 - b.size2 AS size2,
  a.size3 - b.size3 AS size3,
  a.size4 - b.size4 AS size4,
  a.size5 - b.size5 AS size5,
  a.size6 - b.size6 AS size6,
  a.size7 - b.size7 AS size7,
  a.size8 - b.size8 AS size8,
  a.size9 - b.size9 AS size9,
  a.size19 - b.size10 AS size10,
  a.total - b.total AS total,
  a.ref
FROM (
  SELECT ID_OF,
    Col,
    BNT,
    SUM(size1) AS size1,
    SUM(size2) AS size2,
    SUM(size3) AS size3,
    SUM(size4) AS size4,
    SUM(size5) AS size5,
    SUM(size6) AS size6,
    SUM(size7) AS size7,
    SUM(size8) AS size8,
    SUM(size9) AS size9,
    SUM(size10) AS size10,
    SUM(Total) AS Total,
    ref
  FROM tblTailleOFALL
  GROUP BY ID_OF,
    Col,
    BNT,
    ref
  ) a
INNER JOIN (
  SELECT ID_OF,
    Col,
    BNT,
    SUM(size1) AS size1,
    SUM(size2) AS size2,
    SUM(size3) AS size3,
    SUM(size4) AS size4,
    SUM(size5) AS size5,
    SUM(size6) AS size6,
    SUM(size7) AS size7,
    SUM(size8) AS size8,
    SUM(size9) AS size9,
    SUM(size10) AS size10,
    SUM(Total) AS Total,
    ref
  FROM tblTailleALL
  GROUP BY ID_OF,
    Col,
    BNT,
    ref
  ) b ON a.ID_OF = b.ID_OF

答案 4 :(得分:0)

    SELECT a.ID_OF, a.Col, a.BNT, 
      a.size1 - b.size1 as size1, 
      a.size2 - b.size2 as size2,
a.size3 - b.size3 as size3,
a.size4 - b.size4 as size4,
a.size5 - b.size5 as size5,
a.size6 - b.size6 as size6,
a.size7 - b.size7 as size7,
a.size8 - b.size8 as size8,
a.size9 - b.size9 as size9,
a.size10 - b.size10 as size10,
a.Total - b.Total as Total,
a.ref

    (SELECT ID_OF, Col, BNT,    
            SUM(size1) As size1, 
            SUM(size2) As size2,
            SUM(size3) as size3,
            SUM(size4) as size4,
            SUM(size5) as size5,
            SUM(size6)as size6,
            SUM(size7) as size7,
            SUM(size8) as size8,
            SUM(size9) as size9,
            SUM(size10) as size10,
            SUM(Total) as Total,ref 

    FROM tblTailleOFALL 
    GROUP BY ID_OF, Col, BNT, ref) AS a
    inner join 
    (SELECT ID_OF, Col, BNT,    
            SUM(size1) As size1, 
            SUM(size2) As size2,
            SUM(size3) as size3,
            SUM(size4) as size4,
            SUM(size5) as size5,
            SUM(size6) as size6,
            SUM(size7) as size7,
            SUM(size8) as size8,
            SUM(size9) as size9,
            SUM(size10) as size10,
            SUM(Total) as Total,ref 

    FROM tblTailleALL 
    GROUP BY ID_OF, Col, BNT, ref) AS b on a.ID_OF = b.ID_OF