-4

此查询根本没有显示任何内容。我只想要那些在子查询中匹配的 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'))
4

1 回答 1

1

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

在您的代码中,静态列是K. 是的,它是从聚合派生的,但它在子查询中,因此它算作非聚合列。

解决这个问题的方法是将子查询移动到一个公共表表达式中(Oracle 称之为子查询分解),这样可以方便地多次引用该列。Oracle SQL 参考中介绍了 WITH 子句的使用。 了解更多

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

这很可能不是您需要的实际逻辑。您没有对您尝试实现的业务规则进行任何解释,所以我只是猜测如何将子查询与主查询连接起来。

于 2013-02-11T09:53:01.997 回答