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

后端 未结 6 612
野性不改
野性不改 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 15:48

    Since you're using Oracle, you should be able to use AVG() as an analytic (window) function:

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

    No need for subqueries or GROUP BY.

提交回复
热议问题