我的表is--如何计算列的平均值,然后将其包含在oracle中的select查询中?
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);
我知道如何在列成本计算平均,我的代码is--
select avg(cost) from mobile;
和结果是
但是iw蚂蚁计算平均值,然后还显示difference.I能够这样,但是,我不能够在选择query--
我的代码添加平均列---
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
请帮助...
用于提供'create table'和样本数据为'insert',但这不是有效的Oracle语法(Oracle没有'bigint'或'int)。数字不应该放在单引号中! – 2012-03-10 15:55:52
@JustinPihony,你可以使用'[faq]',[faq],'[ask]',[ask]等来代替,请参阅http://stackoverflow.com/editing-help#comment-formatting – Ben 2012-03-10 16:31:45
@Ben谢谢,我一直在寻找评论格式。非常感激。 – 2012-03-10 16:45:40