Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- SELECT
- g.id AS genre_id,
- g.name AS genre_name,
- t2.books_in_genre AS books_in_genre,
- b.id AS book_id,
- b.name AS book_name,
- b.date_added
- FROM genre g
- OUTER JOIN (
- SELECT bg.genre_id AS genre_id,
- COUNT(b.id) as books_in_genre,
- FIRST_VALUE(b.id) OVER (PARTITION BY bg.genre_id ORDER BY b.date_added DESC, b.id DESC) AS newest_book_id
- FROM book b
- LEFT JOIN book_genre bg ON b.id = bg.book_id
- GROUP BY bg.genre_id
- ) t2
- ON g.id = t2.genre_id
- LEFT JOIN books b
- ON b.id = t2.newest_book_id
- ORDER BY books_in_genre DESC
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement