[英]SQL distinct multiple columns
I have a table as following: 我有一张表如下:
book_id author_id mark year
1 1 BAD 2014
1 1 MEDIUM 2014
1 1 GREAT 2015
I would like to execute a query that will give me the best book for each author. 我想执行一个查询,为每位作者提供最好的书。 Something like this: 像这样的东西:
book_id author_id mark year
1 1 GREAT 2015
I tried to use the distinct keyword on multiple fields - but when I do this: 我尝试在多个字段上使用distinct关键字 - 但是当我这样做时:
select distinct book_id, author_id from Books
I get only the book_id and the author_id (as expected) - but I also need the mark and the year - but I cannot add it to the distinct phrase. 我只得到book_id和author_id(如预期的那样) - 但我也需要标记和年份 - 但我不能将它添加到不同的短语中。
Currently I'm using Postgres 9.4 but I need an ANSI-SQL solution. 目前我正在使用Postgres 9.4但我需要一个ANSI-SQL解决方案。
Is there a way I can do that? 有没有办法可以做到这一点?
greatest-n-per-group questions are usually solved using window functions: 每组最大n个问题通常使用窗口函数来解决:
select *
from (
select book_id, author_id, mark, year,
row_number() over (partition by author_id order by case mark when 'GREAT' then 1 when 'MEDIUM' then 2 else 3 end) as rn
from books
) t
where rn = 1;
The above is standard ANSI SQL, but in Postgres using the (proprietary) distinct on
is usually much faster: 以上是标准的ANSI SQL,但在Postgres中使用(专有) distinct on
通常要快得多:
select distinct on (author_id) book_id, author_id, mark, year,
from books
order by author_id,
case mark when 'GREAT' then 1 when 'MEDIUM' then 2 else 3 end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.