ORA-00979:不是GROUP BY表达式?

时间:2010-10-13 07:05:11

标签: sql oracle group-by ora-00979

我已经找到了解决方案,但是如果列中的一个是子查询,我怎样才能将它包含在group by中,或者我是否需要将其包含在group by中。我会在这里粘贴查询..

SELECT s.customerid, s.denomid,
       (SELECT su.quantity
         FROM   stockupdations su
         WHERE  s.customerid = su.custid
         AND    s.denomid = su.denomid
         AND    s.curid = su.curid) AS cur_stock, c.name AS cus_name, d.denomname AS denom,
       cur.curcode AS currency
FROM   stock s
LEFT   JOIN customers c
ON     s.customerid = c.custid
LEFT   JOIN denomination d
ON     d.denomid = s.denomid
LEFT   JOIN currency cur
ON     cur.curid = s.curid
GROUP  BY s.denomid, s.customerid, c.name, d.denomname, cur.curcode
ORDER  BY s.customerid ASC

3 个答案:

答案 0 :(得分:0)

WITH语句怎么样?

WITH tmp AS
(
    SELECT s.customerid, s.denomid,
           c.name AS cus_name,
           d.denomname AS denom,
           cur.curcode AS currency
    FROM   stock s
    LEFT   JOIN customers c
    ON     s.customerid = c.custid
    LEFT   JOIN denomination d
    ON     d.denomid = s.denomid
    LEFT   JOIN currency cur
    ON     cur.curid = s.curid
    GROUP  BY s.denomid, s.customerid, c.name, d.denomname, cur.curcode
    ORDER  BY s.customerid ASC
)
SELECT tmp.customerid, tmp.denomid,
       su.quantity,
       tmp.cus_name,
       tmp.denom,
       tmp.currency
FROM   tmp
INNER  JOIN stockupdations su
ON     tmp.customerid = su.custid
AND    tmp.denomid = su.denomid
AND    tmp.curid = su.curid

答案 1 :(得分:0)

您可以使用from子句中的“内部查询”,而不是select

假设我有一个CUSTOMER表和ORDER表,

我可以拥有像

这样的东西
SELECT C.CUSTOMER_ID, COUNT(T.ORDER_ID)
FROM CUSTOMERS C
JOIN (SELECT CUSTOMER_ID, ORDER_ID, ORDER_DATE, ORDER_STATUS FROM ORDERS O WHERE O.STATUS <> 'DECLINED') T
ON T.CUSTOMER_ID = C.CUSTOMER ID
GROUP BY C.CUSTOMER_ID

(这个SQL只是一个例子,我知道有更好的方法来写这个,但我想不出任何其他的例子)

答案 2 :(得分:0)

您不必一次完成所有事情。尝试将您的查询分成多个部分。子查询,分析函数或其他复杂逻辑看起来像外部查询的简单行。 (不要担心性能问题,如果有意义,Oracle会重新编写它并将所有操作都作为一步完成。)

--Step 3
select [simple values]
from
(
  --Step 2
  select [insanity]
  from
  (
    --Step 1
    select [madness]
    from
    [impossible joins]
  )
)
group by [simple values]