![](/img/trans.png)
[英]How to compare rows in a table and keep the highest values in the row in Oracle
[英]How to calculate a static average value of all rows in a Table to compare individual row values to in Oracle SQL
我需要計算表中所有行的靜態平均值,以比較單個行的值。 到目前為止,這是我想出的:
SELECT i.ITEM_NAME, i.ITEM_PRICE
FROM ITEM i
GROUP BY i.ITEM_NAME, i.ITEM_PRICE
HAVING i.ITEM_PRICE > AVG(i.ITEM_PRICE)
ORDER BY i.ITEM_PRICE DESC, i.ITEM_NAME ASC;
我的問題是,正被用於比較行的平均ITEM_PRICE
對被計算只說行的ITEM_PRICE
(平均一個號碼)。 無論如何,在該查詢中是否可以獲取所有行的ITEM_PRICE
的靜態平均值,以將每行的單個ITEM_PRICE
與之進行比較? 我認為問題可能出在GROUP BY
關聯值的方式上,但我不確定。
由於您只想計算所有行的平均值,因此可以放棄分組,而僅使用WHERE
子句將價格與平均值進行比較。 代碼類似於:
select item_name, item_price
from item
where item_price > (select avg(item_price) from item)
order by item_price desc, item_name asc
當然,這假設一個item_name
僅一行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.