尝试设置子查询以将给定类别的平均价格与所有价格进行比较

wn9m85ua  于 2021-07-29  发布在  Java
关注(0)|答案(1)|浏览(244)

使用 Postgresql 12.2 我正在处理一个表,表中列有餐号(具体餐例)、类型(烹饪类型)、客户id、餐价等。共有6种烹饪类型(意大利、日本等)。我需要找到每种类型菜肴的平均价格,然后显示每种类型的特定用餐ID的价格高于该类型菜肴的平均价格。
当我尝试这个:

select m1.*  
 from meals m1  
 join (select avg(price) 
       from meals  
       group by type) average  
 on meal_id=meal_id  
 where price > average  
 group by type;

我收到错误消息:
错误:运算符不存在:整数>记录行7:where price average
我不知道为什么会收到这个错误消息。谢谢!

li9yvcax

li9yvcax1#

使用窗口功能:

select m.*
from (select m.*, avg(price) over (partition by type) as type_price
      from meals m
     ) m
where price > type_price;

相关问题