ORA-00937: 它不是仅在一个分组上的组函数

ORA-00937: it is not a group function on only one grouping

我必须检索一些数据 (Oracle RDBMS)。我必须过滤它们,将它们分组。所以我想做三个嵌套查询。
从外到内:限制查询对它们进行排序的查询和选择查询(也对它们进行过滤和分组)。 这是查询:

    @SqlQuery("select count(*) personCount, SURNAME surname, SKILL skill, ROWNUM " +
        "     from (select * " +
        "        from (select count(*) personCount, SURNAME surname, SKILL skill from people " +
        "           where ....my filters....
        "          group by SURNAME, SKILL ) " +
        "       order by personCount DESC )  " +
        "     where ROWNUM \<= :limit ")

但它给了我这个错误:ORA-00937: it is not a group function on only one grouping

为什么?

您不能在外部进行计数 select:

   @SqlQuery("select personCount, surname, skill, ROWNUM " +
        "     from (select * " +
        "        from (select count(*) personCount, SURNAME surname, SKILL skill from people " +
        "           where ....my filters.... "
        "          group by SURNAME, SKILL) " +
        "       order by personCount DESC )  " +
        "     where ROWNUM \<= :limit ")

您必须在 group by 子句中指定 select 的所有列 :

    @SqlQuery("select personCount, SURNAME surname, SKILL skill, ROWNUM " +
        "     from (select * " +
        "        from (select count(*) personCount, SURNAME surname, SKILL skill from people " +
        "           where ....my filters.... " +
        "          group by SURNAME, SKILL ) " +
        "       order by personCount DESC )  " +
        "     where ROWNUM \<= :limit ")

另外,personCount的个数您已经统计过了,不用再统计了。

注意我在group by中把knowledge栏换成了skill栏,我以为是打错了。