简体   繁体   中英

count(*) method raises error sql plus

I am trying to use the method count(*) but I always get an error.

here is the code

SQL> select c.cname,avg(o.ord_amt) avg_order_amt , count(*) no_of_order from customer c, orders o  group b
y orderno;

the error is:

ORA-00979: not a GROUP BY expression

I am pretty sure that the mistake is with count(*) method

I need to display the cname no_of_orders, avg_order_amt, where the middle column is total number of order and last is the average order amount for that customer.

Here is the table for customer and orders:

SQL> desc customer
Name                                      Null?    Type
----------------------------------------- -------- ----------------------------

 CUSTNO                                    NOT NULL NUMBER(3)
CNAME                                              VARCHAR2(25)
CITY    
                                                 VARCHAR2(12)

SQL> desc orders
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------

 ORDERNO                                   NOT NULL NUMBER(3)
ODATE                                     NOT NULL DATE
 CUSTNO                                             NUMBER(3)
 ORD_AMT                                            NUMBER(5)

I think that what you're looking for here is:

select c.cname,
       avg(o.ord_amt) avg_order_amt ,
       count(*) no_of_order
from   customer c,
join   orders o on o.customer_id = c.customer_id
group by c.cname;

I'm guessing on how to join those two tables, which you definitely need to do.

Oracle does not work as mysql does that you can group by any column with grouping functions. The error it is giving you is because you are using these grouping functions with a column and you are not grouping by that column.

So, in order to your query work you have to do this:

select c.cname, 
       avg(o.ord_amt) avg_order_amt , 
       count(*) no_of_order 
  from customer c, 
       orders o  
 group by c.cname;

You are also missing a join. The way your query are you will get a cartesian plan between the customer and the orders table which means amount of registries in one times amount of registries on the other.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM