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). 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? I think the problem may be in the way the GROUP BY
correlates the values, but I'm not sure.
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. 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
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.