![](/img/trans.png)
[英]How to calculate average of a column and then include it in a select query in SQL
[英]How to calculate average of a column and then include it in a select query in oracle?
我的桌子是 -
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
請幫忙...
由於您使用的是Oracle,因此您應該能夠使用AVG()作為分析(窗口)函數:
SELECT id, m_name AS "Mobile Name" cost AS Price, AVG(cost) OVER( ) AS Average
, cost - AVG(cost) OVER ( ) AS Difference
FROM mobile
不需要子查詢或GROUP BY。
你的分組是匯總你的平均值,它是整個表的分組(我假設你這樣做是為了允許選擇所有的東西)只需將你的avg移動到另一個子查詢中,刪除總體分組,然后解決它。
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;
當您運行基本的SELECT AVG(cost)
語句時,它自然按指定的列(在這種情況下為SELECT AVG(cost)
分組,因為這是您請求的內容。 我建議閱讀更多關於GROUP BY和聚合的內容 ,以便更好地掌握這個概念。 這應該不僅僅是一個簡單的解決方案。
更新:
下面的答案實際上來自大衛的回答。 它利用了分析功能。 基本上,正在發生的事情是,在每個AVG調用中,您告訴引擎該功能使用什么(在這種情況下,什么都沒有)。 關於分析函數的一個體面的文章可以在這里和這里找到,還有更多關於這個問題的谷歌。
SELECT id, m_name AS "Mobile Name" cost AS Price, AVG(cost) OVER( ) AS Average,
cost - AVG(cost) OVER ( ) AS Difference
FROM mobile
但是,如果您的SQL引擎允許變量,您可以輕松地執行以下答案。 我實際上更喜歡這個用於將來的可維護性/可讀性。 原因是具有良好名稱的變量可以非常描述代碼的未來讀者,而不是需要更多工作來閱讀的分析函數(特別是如果您不理解過度函數)。
此外,此解決方案復制相同的查詢兩次,因此可能值得將您的平均值存儲在SQL變量中。 然后你可以改變你的陳述,只需使用那個全局平均值
這是SQL-Server中的變量(您必須根據自己的SQL實例調整它)
DECLARE @my_avg INT;
SELECT @my_avg = AVG(cost) FROM Mobile;
SELECT id, m_name AS "Mobile Name", cost AS Price,
@my_avg AS Average, cost-@my_avg AS Difference
FROM mobile;
此解決方案也將為您的SQL的未來讀者提供更多清潔
最簡單的變化是將avg(cost) as Average
改為(select avg(cost) from mobile) as Average
。 這也意味着您將不再需要GROUP BY
子句(因為它不會執行您真正想要的操作):
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
;
select pid, name, price as actualcost,
AVERAGE = (select AVG(price) from Part_Master),
price - (select AVG(price) as diff from Part_Master) AS COST_DIFF
from Part_Master
嘗試
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
group by id,m_name,cost;
如果您的查詢太昂貴,那么請給我一個推薦,然后我會改進它。
CROSS JOIN
很少見的適用時間之一:
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
這將導致:
ID Mobile Name PRICE AVERAGE DIFFERENCE
10 NOkia 100 123 -23
11 Samsung 150 123 27
12 Sony 120 123 -3
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.