简体   繁体   English

如何计算表中所有行的静态平均值以将单个行值与Oracle SQL中的值进行比较

[英]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.

相关问题 如何比较表中的行并在Oracle中保留行中的最高值 - How to compare rows in a table and keep the highest values in the row in Oracle 在 SQL 如何比较同一表中两行的值并找到最大值然后 select 是整行 - In SQL how to compare values in two rows in the same table and find the highest value and then select its the entire row Sql,将行值与该列中的行的平均值进行比较 - Sql, compare row values against average of rows in that column 将当前行的值与所有先前行的平均值进行比较 - Compare value of current row to average of all previous rows 如何根据另一列计算以下 n 行的平均值 - SQL (Oracle) - How to calculate the average value of following n rows based on another column - SQL (Oracle) 如何计算表中行的平均值并将特定行添加到输入? - How to calculate average of rows in table and add specifc row to input? 根据 Oracle SQL 中的前一行值计算行值 - Calculate row value based on previous row values in Oracle SQL Oracle SQL 比较列值和 select 行有最大值在列 1 - Oracle SQL to compare column values and select rows there greatest value is in column 1 如何根据另一列计算以下 n 行的平均值 - SQL (Oracle) - 先前回答问题的更新版本 - How to calculate the average value of following n rows based on another column - SQL (Oracle) - updated version of previously answered question 如何计算 SQL 表中每一行与另一个表的所有行的距离? - How do I calculate the distance of each row in an SQL table with all the rows of another table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM