所以我有这个SQL代码:
select stone_number,stone_size,stone_shape,stone_weight
from stone
left Join stone_price stp on stp.id_stone = stone.id_stone
group by stone_number,stone_size,stone_shape,stone_weight
having avg(stp.price) < stp.price;
Sqldeveloper返回:不是按表达式分组,但我的代码中仍按分组。我的目标是获得价格高于所有宝石平均价格的宝石。
您可以使用窗口函数来做到这一点:
select *
from (
select stone_number,stone_size,stone_shape,stone_weight,
stp.price,
avg(stp.price) over () as avg_price
from stone
left Join stone_price stp on stp.id_stone = stone.id_stone
)
where price > avg_price;
请注意,外部联接表上的条件实际上将您的外部联接转换为内部联接。如果您还想包括没有匹配项的行,则stone_price
需要
where price is null
or price > avg_price;
否则,您可以将更left join
改为“普通”join
另一个选项是一个简单的子选择:
select stone_number,stone_size,stone_shape,stone_weight,
from stone
left Join stone_price stp on stp.id_stone = stone.id_stone
where stp.price > (select avg(price) from stone_price);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句