SQL :: Query显示输出错误?

时间:2014-02-04 14:36:26

标签: sql

我正面临下述查询的问题。查询太大了...... 各个查询正确显示数据,即no_of_failures和no_of_success。但是合并后的查询显示任何正确的失败,并且所有记录都没有成功。 有人可以帮我解决这个问题吗?

Query : 
SELECT top 100 sum(no_of_failures) AS TOTAL_FAILURES,
               sum(no_of_success) AS TOTAL_SUCCESSES

FROM
  ( SELECT PAT,
           er.CUST,
           HER3,
           HER2,
           HER1,
           count(*) AS no_of_failures,
           0 AS no_of_success
   FROM
     ( SELECT PAT ,
              CUST ,
              HER3 ,
              HER2 ,
              HER1 ,
              EVN_NO ,
              IMSI
      FROM TABLE_1 rawview
      WHERE DATETIME_ID >= '2014-02-03 11:30'
        AND DATETIME_ID < '2014-02-04 11:30'
        AND IMSI = 460010000
      UNION ALL SELECT PAT ,
                       CUST ,
                       HER3 ,
                       HER2 ,
                       HER1 ,
                       EVN_NO ,
                       IMSI
      FROM TABLE_2 rawview
      WHERE DATETIME_ID >= '2014-02-03 11:30'
        AND DATETIME_ID < '2014-02-04 11:30'
        AND IMSI = 460010000 ) AS er
   GROUP BY PAT,
            er.CUST,
            HER3,
            HER2,
            HER1

   UNION ALL 

             SELECT PAT,
                    suc.CUST,
                    HER3,
                    HER2,
                    HER1,
                    count(*) AS no_of_success,
                    0 AS no_of_failures
   FROM
     ( SELECT PAT ,
              CUST ,
              HER3 ,
              HER2 ,
              HER1 ,
              EVN_NO ,
              IMSI
      FROM TABLE_3 rawview
      WHERE DATETIME_ID >= '2014-02-03 11:30'
        AND DATETIME_ID < '2014-02-04 11:30'
        AND IMSI = 460010000
      UNION ALL SELECT PAT ,
                       CUST ,
                       HER3 ,
                       HER2 ,
                       HER1 ,
                       EVN_NO ,
                       IMSI
      FROM TABLE_4 rawview
      WHERE DATETIME_ID >= '2014-02-03 11:30'
        AND DATETIME_ID < '2014-02-04 11:30'
        AND IMSI = 460010000 ) AS suc
   GROUP BY PAT,
            suc.CUST,
            HER3,
            HER2,
            HER1) DT
GROUP BY DT.PAT,
         DT.CUST,
         DT.HER3,
         HER2,
         DT.HER1 HAVING DT.PAT IS NOT NULL
AND DT.CUST IS NOT NULL
AND DT.HER3 IS NOT NULL
AND DT.HER1 IS NOT NULL
ORDER BY TOTAL_FAILURES DESC

0 个答案:

没有答案