ORA-00979:不是GROUP BY表达式(Case语句问题)

时间:2017-02-03 00:34:52

标签: oracle case aggregate-functions

我在以下查询中收到ORA-00979错误消息。我应该在前面说明我已经阅读了有关Stack Overflow上此错误的许多讨论,但仍无法找到解决方案。我也确信错误来自CASE语句,因为当我完全删除语句时,查询已经产生结果,并且当我删除除了&#;; SUM之外的所有CASE语句时,我也收到了结果。 Z.EQUITY_FOUR)' (也就是说,我也确信EQUITY_FOUR数据库没有问题。)

SELECT 
EXTRACT(YEAR FROM HDS.MORTGAGE_START_DATE) as Yr,
EXTRACT(MONTH FROM HDS.MORTGAGE_START_DATE) as Mth,
Q.CITY as City,
CASE WHEN pp.ISACTIVE = 'NO' 
AND EXTRACT(YEAR FROM HDS.CANCELLATION_DATE) = EXTRACT(YEAR FROM HDS.MORTGAGE_START_DATE) 
AND EXTRACT(MONTH FROM HDS.CANCELLATION_DATE) = EXTRACT(MONTH FROM HDS.MORTGAGE_START_DATE)
THEN SUM(Z.EQUITY_FOUR)
END as Cancelled

(From Statement)

(Where Statement)

GROUP BY EXTRACT(YEAR FROM HDS.MORTGAGE_START_DATE),
     EXTRACT(MONTH FROM HDS.MORTGAGE_START_DATE),
     Q.CITY,
     CASE 
     WHEN pp.ISACTIVE = 'NO' 
     AND EXTRACT(YEAR FROM HDS.CANCELLATION_DATE) = EXTRACT(YEAR FROM HDS.MORTGAGE_START_DATE) 
     AND EXTRACT(MONTH FROM HDS.CANCELLATION_DATE) = EXTRACT(MONTH FROM HDS.MORTGAGE_START_DATE)
     THEN SUM(Z.EQUITY_FOUR)
     END

关于Group BY子句中的CASE语句,在进行我的研究时,我读了这篇文章(ORA-00979: not a GROUP BY expression issue),其中包含两个回复,说明将完整的CASE语句添加到GROUP BY表达式,以及作者发布声明为他的查询工作。但是,我仍然会收到上述错误。

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

我想你想要CASE中的SUM()

SELECT EXTRACT(YEAR FROM HDS.MORTGAGE_START_DATE) as Yr,
       EXTRACT(MONTH FROM HDS.MORTGAGE_START_DATE) as Mth,
       Q.CITY as City,
       SUM(CASE WHEN pp.ISACTIVE = 'NO' AND
                     EXTRACT(YEAR FROM HDS.CANCELLATION_DATE) = EXTRACT(YEAR FROM HDS.MORTGAGE_START_DATE) AND
                     EXTRACT(MONTH FROM HDS.CANCELLATION_DATE) = EXTRACT(MONTH FROM HDS.MORTGAGE_START_DATE)
                THEN Z.EQUITY_FOUR
           END) as Cancelled
(From Statement)
(Where Statement)
GROUP BY EXTRACT(YEAR FROM HDS.MORTGAGE_START_DATE),
         EXTRACT(MONTH FROM HDS.MORTGAGE_START_DATE),
         Q.CITY;