0

我有两张桌子:

•表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

问题:我如何合并这两个查询以返回所有作者 + 最新出版书籍的作者?

4

1 回答 1

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
于 2013-10-11T07:44:13.923 回答