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

后端 未结 6 611
野性不改
野性不改 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:01

    One of the rare times a CROSS JOIN is applicable:

    WITH avgcost as (select round(avg(cost)) as Average from mobile)
    SELECT id, m_name as "Mobile Name", cost as Price, Average,
    cost-Average as Difference
    FROM mobile cross join avgcost
    

    Which will result in:

    ID  Mobile Name PRICE   AVERAGE DIFFERENCE
    10  NOkia       100     123     -23
    11  Samsung     150     123     27
    12  Sony        120     123     -3
    

提交回复
热议问题