Query to return the items that meet the criteria for being the latest.
I want to return the last four issues of a book (I have circled the ones in question) dynamically (not hard coded). For example if the new book called SHBb19 is released then SHBb18 must be not displayed on the list. I need this to always display last four issues of each book. Thank you
You could either do a rank of the book-issue, ordered by the issue in descending order, and then pick those with rank=1. WITH CTE AS ( SELECT BookIssue, RANK() OVER(ORDER BY BookIssue DESC) as BookIssueRank FROM Books )SELECT BookIssue FROM CTE WHERE BookIssueRank=1; Or you could let a sub-select get the max(bookissue) and use that in the where clause. SELECT BookIssue FROM Books WHERE BookIssue = (SELECT MAX(BookIssue) FROM books)