我想知道有多少商品价格高于平均价格且价格较低。
我的查询:
SELECT COUNT(*)
FROM item i
WHERE refine = 0 AND item = 5170
GROUP BY id
HAVING price > AVG(price) - STD(price) AND i.price < AVG(price) + STD(price)
但我在'having clause'
中收到错误未知栏'价格'如果我在SELECT中使用AVG和STD函数:
SELECT COUNT(*), SUM(price > AVG(price) - STD(price) AND i.price < AVG(price) + STD(price)) as middle
FROM item i
WHERE refine = 0 AND item = 5170
我再次收到错误。如何进行正确的查询?
此处链接sqlFiddle
答案 0 :(得分:2)
试试这个
select count(*) from item as t1 inner join
(
select id,AVG(price) - STD(price) as sum1, AVG(price) + STD(price) as sum2
from item
group by id
) as t2
on t1.id=t2.id and t1.price>sum1 and t1.price<sum2
答案 1 :(得分:-1)
SELECT COUNT(*), SUM(price > AVG(price)) - STD(price) AND i.price < AVG(price) + STD(price)) as middle
FROM item i WHERE refine = 0 AND item = 5170
你忘了“)” - &gt; SUM(price > AVG(price))