[英]How to calculate a static average value of all rows in a Table to compare individual row values to in Oracle SQL
I need to calculate a static average value of all rows in a Table to compare individual row values against. 我需要计算表中所有行的静态平均值,以比较单个行的值。 Here is what I have come up with so far:
到目前为止,这是我想出的:
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;
My problem is that the average that is being used to compare a row's ITEM_PRICE
against is being calculated for only said row's ITEM_PRICE
(an average of one number). 我的问题是,正被用于比较行的平均
ITEM_PRICE
对被计算只说行的ITEM_PRICE
(平均一个号码)。 Is there anyway within this query that I could obtain a static average of all of the rows' ITEM_PRICE
s to compare the individual ITEM_PRICE
for each row against? 无论如何,在该查询中是否可以获取所有行的
ITEM_PRICE
的静态平均值,以将每行的单个ITEM_PRICE
与之进行比较? I think the problem may be in the way the GROUP BY
correlates the values, but I'm not sure. 我认为问题可能出在
GROUP BY
关联值的方式上,但我不确定。
Since you only want to calculate the average of all rows, you could forego grouping, and just use a WHERE
clause to compare the price against average. 由于您只想计算所有行的平均值,因此可以放弃分组,而仅使用
WHERE
子句将价格与平均值进行比较。 Code would be something like: 代码类似于:
select item_name, item_price
from item
where item_price > (select avg(item_price) from item)
order by item_price desc, item_name asc
Of course, this assumes that you have only one row for one item_name
. 当然,这假设一个
item_name
仅一行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.