【问题标题】:From subquery giving error in oracle来自oracle中的子查询给出错误
【发布时间】:2015-08-09 16:40:22
【问题描述】:

我有这个问题:

SELECT T.custno,T.custlastname,AVG(T.OrderAmount) , T.OrderCount
FROM(
      SELECT  A.custno,A.custlastname,count(b.ordno) as OrderCount,    sum(c.qty*d.prodprice) AS  OrderAmount
  FROM customer A 
      JOIN ordertbl B ON A.custno=b.custno
      JOIN ordline C ON b.ordno=c.ordno
      JOIN  product D ON c.prodno=d.prodno
  WHERE A.custstate='CO'
  GROUP BY A.custno,A.custlastname, b.ordno) AS T
GROUP BY T.custno,T.custlastname;

我得到这个错误:

ORA-00933: SQL command not properly ended

当我显式执行内部子查询时,它运行良好。请告诉我原因。

可以在http://sqlfiddle.com/#!4/6b48d/12尝试

【问题讨论】:

    标签: sql database oracle oracle11g


    【解决方案1】:

    我猜您在外部查询的group by 子句中缺少一列。试试下面的查询

    SELECT T.custno,T.custlastname,AVG(T.OrderAmount) , T.OrderCount
           FROM (SELECT  A.custno, 
                        A.custlastname,
                        count(b.ordno) as OrderCount,
                        sum(c.qty*d.prodprice) AS  OrderAmount
                 FROM customer A 
                 JOIN ordertbl B ON A.custno=b.custno
                 JOIN ordline C ON b.ordno=c.ordno
                 JOIN  product D ON c.prodno=d.prodno
                 WHERE A.custstate='CO'
                 GROUP BY A.custno,A.custlastname, b.ordno) T
           GROUP BY T.custno,T.custlastname,T.OrderCount;
    

    【讨论】:

    • 不,还是一样的结果:(
    【解决方案2】:

    试试这个:

    SELECT T.custno,T.custlastname,avg(T.OrderAmount) , T.OrderCount
           FROM (SELECT  A.custno, 
                        A.custlastname,
                        count(b.ordno) as OrderCount,
                        sum(c.qty*d.prodprice) AS OrderAmount
                 FROM customer A 
                 JOIN ordertbl b ON A.custno=b.custno
                 JOIN ordline c ON b.ordno=c.ordno
                 JOIN  product d ON c.prodno=d.prodno
                 WHERE A.custstate='CO'
                 GROUP BY A.custno,A.custlastname,b.ordno,OrderAmount,
                 OrderCount) AS T)
           GROUP BY T.custno,T.custlastname,T.OrderAmount,T.OrderCount;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-14
      • 2015-09-21
      • 1970-01-01
      • 1970-01-01
      • 2015-03-06
      相关资源
      最近更新 更多