Groups | Blog | Home
all groups > sql server programming > january 2004 >

sql server programming : Query to group latest posts by author


Cyrill Vatomsky
1/29/2004 10:30:57 PM
Here is a simplified version:

Table Authors: AuthorID(int) Name(varchar)

Table Articles artID(int), authorID(int, foreign key to Authors), Date
(smalldatetime), ArtTitle (varchar)

The trick is that authors publish randomly, one can publish every day,
another once a week, yet another once a month.

I need to get list of authors with 5 latest articles each, sorted first by
author name and then by date.

Thanks!


Anith Sen
1/30/2004 12:44:28 AM
DDLs & sample data along with expected results would have been useful. In
any case, how about:

SELECT *
FROM Articles a1
INNER JOIN Authors a2
ON a1.authorID = a2.authorID
WHERE a1.dt IN ( SELECT TOP 5 a3.dt --- add WITH TIES if needed
FROM Articles a3
WHERE a3.authorID = a1.authorID
ORDER BY a3.dt DESC )
ORDER BY a2.Name, a1.dt ;

--
- Anith
( Please reply to newsgroups only )

Cyrill Vatomsky
1/30/2004 8:06:19 AM
Thank you, it worked great!

[quoted text, click to view]

AddThis Social Bookmark Button