How to join only one row in joined table with postgres?

后端 未结 7 501
清歌不尽
清歌不尽 2021-01-31 01:31

I have the following schema:

CREATE TABLE author (
    id   integer
  , name varchar(255)
);
CREATE TABLE book (
    id        integer
  , author_id integer
  ,          


        
7条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-31 02:27

    create temp table book_1 as (
    SELECT
    id
    ,title
    ,author_id
    ,row_number() OVER (PARTITION BY id) as rownum 
    FROM
    book)  distributed by ( id );
    
    select author.id,b.id, author.id, author.name, b.title as last_book
    from
        author
    
        left  join
       (select * from  book_1 where rownum = 1 ) b on b.author_id = author.id
    order by author.id, b.id desc
    

提交回复
热议问题