How to calculate average of a column and then include it in a select query in oracle?

后端 未结 6 608
野性不改
野性不改 2020-12-30 14:55

My table is--

create table mobile
(
  id integer,
  m_name  varchar(20),
  cost integer
)

and the values are --

insert into         


        
6条回答
  •  旧巷少年郎
    2020-12-30 16:00

    The simplest change is to change avg(cost) as Average to (select avg(cost) from mobile) as Average. This also means that you won't need the GROUP BY clause anymore (since it doesn't do what you actually wanted):

    SELECT id,
           m_name AS "Mobile Name",
           cost AS "Price",
           (SELECT AVG(cost) FROM mobile) AS "Average",
           cost - (SELECT AVG(cost) FROM mobile) AS "Difference"
      FROM mobile
    ;
    

提交回复
热议问题