ORA-00979,但我的 GROUP BY 语句中的 SELECT 中有元素

ORA-00979, but I have elements in my SELECT in my GROUP BY statement

我有一个类似的查询:

SELECT field1,
       field2,
       field3,
       field4,
       field5,
       field6,
       field7,
       field8,
       field9,
       count(field9)
  FROM REPORT_TABLE
       LEFT JOIN
       DATAMINE
         USING (REPORT_ID)
 WHERE field1 LIKE 'MatchingText%'
   AND TS_START between to_date('2015-05-01', 'YYYY-MM-DD') and to_date('2015-06-06', 'YYYY-MM-DD') + 0.99999999
GROUP BY field9,
         1,
         2,
         3,
         4,
         5,
         6,
         7,
         8,
         9;

当我重新运行它时,我得到

ORA-00979: not a GROUP BY expression
00979. 00000 -  "not a GROUP BY expression"

我看到了 this 问题,解释说我需要在 group by 中包含 select 中的所有字段。我通过列号来做到这一点。

如何调整此查询以免出现 00979 错误?

I've done that by having the column numbers.

这适用于像 PostgreSQL 这样的数据库:

SELECT abc, xyz
FROM ...
GROUP BY 1, 2 -- referencing abc and xyz by column number

但在 Oracle 中没有。在 Oracle 中,您不能从 GROUP BY 子句中引用 SELECT 子句,但您必须再次重复完整的列表达式

SELECT abc, xyz
FROM ...
GROUP BY abc, xyz

...或者您的情况:

[...]
GROUP BY 
    field1,
    field2,
    field3,
    field4,
    field5,
    field6,
    field7,
    field8,
    field9;

我相信您应该能够按非计数或聚合字段进行分组,而不会出现错误。您可能还需要添加命名约定,因为您正在计算 field9 并按它计算

SELECT 
field1,
field2,
field3,
field4,
field5,
field6,
field7,
field8,
field9,
count(field9)  AS "Field9_Count"
FROM REPORT_TABLE
LEFT JOIN DATAMINE
    USING (REPORT_ID)
WHERE  ( field1 LIKE 'MatchingText%') AND TS_START between to_date('2015-05-01', 'YYYY-MM-DD') and to_date('2015-06-06', 'YYYY-MM-DD')+0.99999999 
GROUP BY field1, field2, field3, field4, field5, field6, field7, field8, field9