ora-00979不是GROUP BY表达式错误

时间:2013-06-27 14:53:45

标签: sql oracle oracle11g ora-00979

我在查询

后面运行上面的错误
SELECT TO_CHAR(EVENTDATA.TIME_STAMP,'YYYY/IW'),
  COUNT(EVENT_TYPE)
FROM EVENTDATA
INNER JOIN CLIENTDATA
ON CLIENTDATA.CLIENT_ID        = EVENTDATA.EVENT_ID
WHERE CLIENTDATA.CLIENT_STATUS = 0
AND EVENTDATA.EVENT_TYPE       = 0
AND EVENTDATA.time_stamp       > to_date('2013-04-03 20:03:20', 'yyyy-mm-dd HH24:MI:SS')
GROUP BY TO_CHAR(EVENTDATA.TIME_STAMP,'YYYY/IW')
ORDER BY TO_CHAR(EVENTDATA.TIME_STAMP,'YYYY/IW')

以下是表格的创建方式

CREATE TABLE "DIMA"."CLIENTDATA"
  (
    "CLIENT_ID"     VARCHAR2(256 BYTE) NOT NULL ENABLE,
    "CLIENT_STATUS" NUMBER(10,0) NOT NULL ENABLE,
    CONSTRAINT "CLIENTDATA_PK" PRIMARY KEY ("CLIENT_ID") 
  );

CREATE TABLE "DIMA"."EVENTDATA"
  (
    "EVENTDATA_ID"    NUMBER NOT NULL ENABLE,
    "EVENT_ID"        VARCHAR2(256 BYTE),
    "EVENT_TYPE"      NUMBER(10,0) NOT NULL ENABLE,
    "TIME_STAMP" DATE,
    CONSTRAINT "EVENTDATA_PK" PRIMARY KEY ("EVENTDATA_ID") 
  );

我正在通过ojbc驱动程序运行查询并获得了下面的Java堆栈跟踪

Error executing SQL query "SELECT TO_CHAR(EVENTDATA.TIME_STAMP,'YYYY/IW'),   COUNT(EVENT_TYPE) FROM EVENTDATA INNER JOIN CLIENTDATA ON CLIENTDATA.CLIENT_ID        = EVENTDATA.EVENT_ID WHERE CLIENTDATA.CLIENT_STATUS = 0 AND EVENTDATA.EVENT_TYPE       = 0 AND EVENTDATA.time_stamp       > to_date('2013-04-03 20:03:20', 'yyyy-mm-dd HH24:MI:SS') GROUP BY TO_CHAR(EVENTDATA.TIME_STAMP,'YYYY/IW') ORDER BY TO_CHAR(EVENTDATA.TIME_STAMP,'YYYY/IW')". err: java.sql.SQLSyntaxErrorException: ORA-00979: not a GROUP BY expression

    at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:439)
    at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:395)
    at oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:802)
    at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:436)
    at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:186)
    at oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.java:521)
    at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:205)
    at oracle.jdbc.driver.T4CPreparedStatement.executeForDescribe(T4CPreparedStatement.java:861)
    at oracle.jdbc.driver.OracleStatement.executeMaybeDescribe(OracleStatement.java:1145)
    at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1267)
    at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3449)
    at oracle.jdbc.driver.OraclePreparedStatement.executeQuery(OraclePreparedStatement.java:3493)
    at oracle.jdbc.driver.OraclePreparedStatementWrapper.executeQuery(OraclePreparedStatementWrapper.java:1491)
    at org.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:92)
    at org.datanucleus.store.rdbms.SQLController.executeStatementQuery(SQLController.java:450)
    at org.datanucleus.store.rdbms.query.SQLQuery.performExecuteInternal(SQLQuery.java:410)
    at org.datanucleus.store.query.Query$1.run(Query.java:1652)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.lang.Thread.run(Thread.java:619)

看起来我将SELECT的所有列都放在GROUP BY语句中,除了分组函数1(COUNT)。

我该如何解决这个问题。 谢谢,迪马

1 个答案:

答案 0 :(得分:0)

我太懒了,不能测试你的陈述。

我建议使用subselect。这应该有效:

SELECT 
  D.EVENT_TIME,
  COUNT(D.EVENT_TYPE) AS EVENT_TYPE_COUNT
FROM (
  SELECT 
    TO_CHAR(EVENTDATA.TIME_STAMP,'YYYY/IW') AS EVENT_TIME,
    EVENT_TYPE
  FROM EVENTDATA
  INNER JOIN CLIENTDATA
  ON CLIENTDATA.CLIENT_ID        = EVENTDATA.EVENT_ID
  WHERE CLIENTDATA.CLIENT_STATUS = 0
  AND EVENTDATA.EVENT_TYPE       = 0
  AND EVENTDATA.time_stamp       > to_date('2013-04-03 20:03:20', 'yyyy-mm-dd HH24:MI:SS')
) D
GROUP BY D.EVENT_TIME
ORDER BY D.EVENT_TIME;