【发布时间】:2018-09-14 10:48:14
【问题描述】:
我对 SQL 很陌生,我试图只显示最昂贵的书的一个类别。我的第一个想法是从select 中删除max(books.purchase_price) 并将max(books.purchase_price) = books.purchase_price 添加到where 部分。但这给了我一个错误信息。
select categories.category, max(books.purchase_price) as price
from books, categories
where categories.category_id = books.category_id
group by categories.category
order by pris desc
limit 1
只有两个表:books,其中包含标题、价格和category_id 的条目;和 categories 具有 category_id 和不同的类别(名称)。
【问题讨论】:
-
当前代码“有效”,但给了我类别和最高价格。我只想显示最贵的书的类别名称。
-
你应该检查this。
-
如果有两本(或更多)书的最高价格相同,预期的结果是什么?
标签: sql postgresql greatest-n-per-group