0

我在运行以下查询时遇到错误

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 语句中,除了分组函数(COUNT)。

我该如何解决。谢谢,迪玛

4

1 回答 1

0

我懒得测试你的说法。

我建议使用子选择。这应该有效:

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;
于 2013-06-27T15:34:47.573 回答