If I have a table of data like this
tableid author book pubdate 1 1 The Hobbit 1923 2 1 Fellowship 1925 3 2 Foundation Trilogy 1947 4 2 I Robot 1942 5 3 Frankenstein 1889 6 3 Frankenstein 2 1894
Is there a query that would get me the following without having to use a temp table, table variable or cte?
tableid author book pubdate 1 1 The Hobbit 1923 4 2 I Robot 1942 5 3 Frankenstein 1889
So I want min(ranking) grouping by person and ending up with book for that min(ranking) value.
OK, the data I gave initially was flawed. Instead of a ranking column I’ll have a date column. I need the book published earliest by author.
Missed that a CTE was not valid (but not sure why). How about as a subquery?
Original:
If you want to return multiple rows when there is a tie for earliest book, use RANK() in place of ROW_NUMBER(). In the case of a tie and you only want to return one row, you need to add additional tie breaker columns to the ORDER BY within OVER().