不是单一的群体功能

时间:2013-02-11 08:52:37

标签: oracle aggregate-functions

此查询根本没有显示任何内容。我想要的结果只是那些在子查询中匹配的ORDERNO。

SELECT SUM(STY.NQTY * STY.NUNITPRICE   
             / 1000) AS ORDERAMOUNT,
    (SELECT SUM(TBLORDERSTYLE.NQTY * TBLORDERSTYLE.NUNITPRICE/1000) FROM TBLORDER
    INNER JOIN TBLORDERSTYLE
    ON TBLORDER.CORDERNO = TBLORDERSTYLE.CORDERNO
    WHERE TBLORDER.CLCNO LIKE '%MR#%' AND
    TBLORDER.CORDERNO = ORD.CORDERNO
    GROUP BY ORD.CORDERNO) AS K
          FROM TBLORDER ORD INNER JOIN   
          TBLORDERSTYLE STY ON    
          ORD.CORDERNO = STY.CORDERNO
          where (ORD.NPOSTFLAG = '1') AND   
           (ORD.NCANCEL = '0') AND
    ORD.NPAYMODE = '1'  AND  
          (ORD.DPICONFIRMDATE  BETWEEN    
          TO_DATE('01-JUL-2012')AND TO_DATE('31-JUL-2012'))

1 个答案:

答案 0 :(得分:1)

错误消息非常明确:您没有GROUP BY子句。每当我们将SUM()等聚合函数与非聚合列混合时,我们需要在GROUP BY子句中包含静态列。

在您的代码中,静态列为K。是的,它来自聚合,但是在子查询中,因此它被视为非聚合列。

解决这个问题的方法是将子查询移动到公共表表达式(Oracle调用子查询因子),这样可以更容易地多次引用该列。 Oracle SQL Reference中介绍了WITH cluase的使用。 Find out more

with cte as (SELECT ORD.CORDERNO
                   , SUM(TBLORDERSTYLE.NQTY * TBLORDERSTYLE.NUNITPRICE/1000) as k
    FROM TBLORDER
    INNER JOIN TBLORDERSTYLE
    ON TBLORDER.CORDERNO = TBLORDERSTYLE.CORDERNO
    WHERE TBLORDER.CLCNO LIKE '%MR#%' AND
    TBLORDER.CORDERNO = ORD.CORDERNO
    GROUP BY ORD.CORDERNO)
SELECT SUM(STY.NQTY * STY.NUNITPRICE   
             / 1000) AS ORDERAMOUNT,
      cte.K
          FROM cte inner join
          TBLORDER ORD on ord.orderno = k.orderno
          INNER JOIN TBLORDERSTYLE STY ON STY.CORDERNO  = k.orderno
          where (ORD.NPOSTFLAG = '1') AND   
           (ORD.NCANCEL = '0') AND
           ORD.NPAYMODE = '1'  AND  
          (ORD.DPICONFIRMDATE  BETWEEN TO_DATE('01-JUL-2012')AND TO_DATE('31-JUL-2012'))
group by cte.k

这很可能不是您需要的实际逻辑。您没有包含您尝试实现的业务规则的任何解释,所以我刚刚猜测了如何将子查询与主查询相结合。