I'd like to form an sql query, which returns some data of rows, which have max value in some group. Consider the following example for a demonstration:
There are three tables: country, publisher and book. Each publisher belongs to one country and each book has one publisher. Definitions could look like
Country(country_id, country_name)
Publisher(pub_id, pub_name, fk_pub_country)
Book(book_id, book_name, release_date, fk_book_publisher)
I'd like to select (country_id, book_name) grouped by country, so that each row contains the name of the most recently released book in that country. If there are multiple books released on the same day, I should get the one with the highest id.
If I just use group by -clause and max, I cannot include the book name. If I select view (country_id, max_date) and join it with publisher and book, I may receive multiple rows per country. How could I achieve the desired result?