ORA-00979:不是GROUP BY表达式

时间:2016-06-15 06:52:33

标签: java sql oracle hibernate

我在DAO实现中创建了用于运行查询的函数

SQL QUERY:

  SELECT pct.IA_FILING_METHOD_CODE_ID, COUNT(*) 
            FROM Ia_Dossier pct 
            WHERE 1=1 and pct.INTERNATIONAL_FILING_DATE IS NOT NULL 
            AND pct.INTERNATIONAL_FILING_DATE>='11-SEP-08' 
            AND pct.INTERNATIONAL_FILING_DATE<='11-SEP-16' 
            GROUP BY pct.IA_FILING_METHOD_CODE_ID 
            HAVING pct.IA_FILING_METHOD_CODE_ID IS NOT NULL 

这在db中正常运行但从hibernate代码运行时出错。

功能:

    @Override
    public List<Object[]> findStatsByROGroupedByFileFormat(Date startDate, Date endDate,String RO, String groupBy) {

        StringBuilder jpqlQuery = new StringBuilder();
        jpqlQuery.append("Select pct.").append(groupBy).append(",count(*) from IaDossier as pct where 1=1 and pct.internationalFilingDate is not null");
        if(startDate!=null)
        {
            jpqlQuery.append(" and pct.internationalFilingDate >=:startDate");
        }
        if(endDate!=null)
        {
            jpqlQuery.append(" and pct.internationalFilingDate <=:endDate");
        }

        if(groupBy!=null && RO!=""){
            jpqlQuery.append(" group by pct.").append(groupBy);
            jpqlQuery.append(" having pct.").append(groupBy).append(" is not null");
        }
System.out.println(jpqlQuery+"hc");
        Query q = entityManager.createQuery(jpqlQuery.toString());
        if(startDate!=null)
        {
            q.setParameter("startDate", startDate);
        }
        if(endDate!=null)
        {
            q.setParameter("endDate", endDate);
        }
        return (List<Object[]>)q.getResultList();
    }

CONSOLE:

12:09:45,572 INFO  [stdout] (http-localhost-127.0.0.1-8082-2) Select pct.iaFilingMethod,count(*) from IaDossier as pct where 1=1 and pct.internationalFilingDate is not null and pct.internationalFilingDate >=:startDate and pct.internationalFilingDate <=:endDate group by pct.iaFilingMethod having pct.iaFilingMethod is not null

12:09:45,730 INFO  [stdout] (http-localhost-127.0.0.1-8082-2) Hibernate: select iadossier0_.IA_FILING_METHOD_CODE_ID as col_0_0_, count(*) as col_1_0_, iafilingme1_.IA_FILING_METHOD_CODE_ID as IA1_52_, iafilingme1_.EN_NAME as EN2_52_, iafilingme1_.END_DATE as END3_52_, iafilingme1_.FR_NAME as FR4_52_, iafilingme1_.IS_EFILING as IS5_52_, iafilingme1_.OFFICE_RESTRICTIONS_APPLY as OFFICE6_52_, iafilingme1_.START_DATE as START7_52_ from IA_DOSSIER iadossier0_ inner join IBPROD.IA_FILING_METHODS iafilingme1_ on iadossier0_.IA_FILING_METHOD_CODE_ID=iafilingme1_.IA_FILING_METHOD_CODE_ID where 1=1 and (iadossier0_.INTERNATIONAL_FILING_DATE is not null) and iadossier0_.INTERNATIONAL_FILING_DATE>=? and iadossier0_.INTERNATIONAL_FILING_DATE<=? group by iadossier0_.IA_FILING_METHOD_CODE_ID having iadossier0_.IA_FILING_METHOD_CODE_ID is not null

12:09:46,076 WARN  [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (http-localhost-127.0.0.1-8082-2) SQL Error: 979, SQLState: 42000
12:09:46,078 ERROR [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (http-localhost-127.0.0.1-8082-2) ORA-00979: not a GROUP BY expression

我不知道为什么在查询在sql developer中正常运行时它会出现GROUP BY错误。

3 个答案:

答案 0 :(得分:2)

你想做什么?

使用HAVING子句时,必须在其中指定的列上使用聚合函数,因为您要对此查询进行分组。我只是将其更改为简单的WHERE子句,无需在HAVING中指定。

SELECT pct.IA_FILING_METHOD_CODE_ID, COUNT(*) 
FROM Ia_Dossier pct 
WHERE pct.INTERNATIONAL_FILING_DATE IS NOT NULL 
    AND pct.INTERNATIONAL_FILING_DATE>='11-SEP-08' 
    AND pct.INTERNATIONAL_FILING_DATE<='11-SEP-16'
    AND pct.IA_FILING_METHOD_CODE_ID IS NOT NULL
 GROUP BY pct.IA_FILING_METHOD_CODE_ID 
  

Oracle HAVING子句与GROUP BY子句结合使用,将返回行组限制为条件为TRUE的行。

答案 1 :(得分:1)

如果运行sql代码,数据库中显示的数据是什么? 可能是您的pct.IA_FILING_METHOD_CODE_IDNULL

您必须将SELECT的所有列放在GROUP BY中,或使用其中的函数将结果压缩为单个值(例如MINMAX或{ {1}})。

尝试打印整个SUM并将其与您在数据库中使用的SQL进行比较。 (同样在这里发布两个SQL查询)

有关GROUP BY

的更多信息

答案 2 :(得分:0)

我终于明白了这个错误的原因,在我的jpa实体中,IA_FILING_METHOD_CODE_ID被设置为fetch.EAGER,这就是为什么它自动连接到其他表,非常感谢帮助它将我带到了正确的方向。 :)