从子查询中给出了oracle中的错误

时间:2015-08-09 16:40:22

标签: sql database oracle oracle11g

我有这个问题:

ORA-00933: SQL command not properly ended

我收到此错误:

contravariant

当我显式执行内部子查询时,它运行正常。请让我知道原因。

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

2 个答案:

答案 0 :(得分: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;

答案 1 :(得分:0)

试试这个:

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;