我有两张桌子:
•表1:Books
•表2:Authors
我有以下查询:(此查询返回所有具有书名和发布日期的作者)
SELECT Authors.AuthorName, Books.BookTitle, Books.DatePublished, Books.Author
FROM Authors LEFT OUTER JOIN Books ON Authors.AuthorID = Books.Author
问题:!!
我想归还所有作者(两本有书出版的人和那些没有出版书籍的人。
条件(有书的作者。只返回最新出版的书)
我可以通过以下查询获得最新出版的书籍:
SELECT a.*
FROM [Books] a left outer join Books b on a.Author = b.author and
a.DatePublished < b.DatePublished where b.ISDN is null
问题:如何将这两个查询合并以返回所有 作者+作者最新出版的书?
答案 0 :(得分:0)
请尝试:
SELECT DISTINCT
AuthorName,
BookTitle,
DatePublished,
Author
FROM(
SELECT
Authors.AuthorName,
Books.BookTitle,
Books.DatePublished,
Books.Author,
ROW_NUMBER() over (partition by Authors.AuthorID order by DatePublished desc) Rnum
FROM Authors LEFT OUTER JOIN Books ON Authors.AuthorID = Books.Author
)x WHERE Rnum=1