我该如何删除结果,使我得到零总和,而不使用子查询?

时间:2019-01-23 19:10:34

标签: sql oracle

我的总和不能为零。所以我用一个子查询来实现它。 问题在于子查询不是很快。您将如何提高效率?

select * from (
      SELECT a,
             b, 
             c, 
             sum(d) as  ftd 
            FROM items f 
            WHERE f.category = 'ANY' AND 
                  f.version = 'V2018' AND 
                  f.month = '01' AND 
                  f.year = '2017'
            Group by  f.a, 
                     f.b, 
                     f.c 
            ORDER BY f.a, 
                     f.b, 
                     f.c
    )where ftd!=0;  

2 个答案:

答案 0 :(得分:3)

使用HAVING

  SELECT a,
         b, 
         c, 
         sum(d) as  ftd 
        FROM items f 
        WHERE f.category = 'ANY' AND 
              f.version = 'V2018' AND 
              f.month = '01' AND 
              f.year = '2017'
        Group by  f.a, 
                 f.b, 
                 f.c 
       HAVING sum(d) != 0

答案 1 :(得分:0)

您可以使用HAVING子句解决此问题:

SELECT a,
b, 
c, 
sum(d) as  ftd 
FROM items f 
WHERE f.category = 'ANY' AND 
f.version = 'V2018' AND 
f.month = '01' AND 
f.year = '2017'
Group by  f.a, 
f.b, 
f.c 
HAVING SUM(f.d)<>0
ORDER BY f.a, 
f.b, 
f.c