ORACLE GROUP BY错误

时间:2014-03-12 07:20:40

标签: sql oracle

我的SQL查询如下:

SELECT 
  oh.ORDER_NUMBER, oh.CREATED_TS, stat.STATE, stat.TIMESTAMP 
FROM 
 IMPL_OH_ORDER_HEADER oh,
 SNCR_ORDER_DISPOSITION stat,
 SNCR_ORDER_DISP_HEAD stathead,
 IMPL_CUST_ACCOUNT cust 
WHERE 
 stat.DISP_TRANSACTION_ID=stathead.DISP_TRANSACTION_ID
 and stathead.TRANSACTION_ID=oh.TRANSACTION_ID 
 and oh.ACCOUNT_ID=cust.ACCOUNT_ID 
 and stat.CATEGORY=100 
 and oh.USER_ID='a' 
 and cust.ACCOUNT_NUM='123'
 GROUP BY oh.ORDER_NUMBER
 ORDER BY stat.TRX_SEQ DESC;

出现以下错误:ORA-00979: not a GROUP BY expression

如果我删除GROUP BY表达式,我就不会收到错误。

什么是正确的GROUP BY语法?

研究:谷歌搜索说,如果使用AGGREGATE函数,我应该在GROUP BY子句中使用聚合函数不使用的所有字段。所以我尝试将GROUP BY表达式修改为GROUP BY oh.ORDER_NUMBER, oh.CREATED_TS, stat.TIMESTAMP, stat.STATE

然而我得到同样的错误。

请帮忙。

2 个答案:

答案 0 :(得分:2)

您收到错误是因为列表中的订单中有STAT.TRX_SEQ

如果 group by 子句与 order by 子句一起使用,则必须按照您分组的列或聚合值进行排序。

此规则也适用于选定的值:它们是group by或aggregate函数(或伪列)的一部分。

答案 1 :(得分:0)

你能试试这个吗?看看??

SELECT   OH.ORDER_NUMBER,
         OH.CREATED_TS,
         STAT.STATE,
         STAT.TIMESTAMP
FROM     IMPL_OH_ORDER_HEADER OH,
         SNCR_ORDER_DISPOSITION STAT,
         SNCR_ORDER_DISP_HEAD STATHEAD,
         IMPL_CUST_ACCOUNT CUST
WHERE    STAT.DISP_TRANSACTION_ID = STATHEAD.DISP_TRANSACTION_ID
AND      STATHEAD.TRANSACTION_ID = OH.TRANSACTION_ID
AND      OH.ACCOUNT_ID = CUST.ACCOUNT_ID
AND      STAT.CATEGORY = 100
AND      OH.USER_ID = 'A'
AND      CUST.ACCOUNT_NUM = '123'
GROUP BY OH.ORDER_NUMBER,
         OH.CREATED_TS,
         STAT.STATE,
         STAT.TIMESTAMP
ORDER BY STAT.TRX_SEQ DESC;