我的桌子是——
create table mobile
(
id integer,
m_name varchar(20),
cost integer
)
其值为——
insert into mobile values(10,'NOkia',100);
insert into mobile values(11,'Samsung',150);
insert into mobile values(12,'Sony',120);
我知道如何计算列成本的平均值,我的代码是--
select avg(cost) from mobile;
结果是123
但我想计算平均值,然后也显示差异。我能够做到这一点,但是我无法在选择查询中添加 avg 列 -
我的代码是——
SELECT id, m_name as "Mobile Name", cost as Price,avg(cost) as Average,
cost-(select avg(cost) from mobile) as Difference FROM mobile
group by id,m_name,cost;
输出是——
id Mobile Name Price Average Difference
10 Nokia 100 100 -23
11 Samsung 150 150 27
12 Sony 120 120 -3
我想要的是纠正这个平均列..我想要这个---
id Mobile Name Price Average Difference
10 Nokia 100 123 -23
11 Samsung 150 123 27
12 Sony 120 123 -3
请帮忙...