Sqlserver
 sql >> Base de données >  >> RDS >> Sqlserver

obtenir les 10 meilleurs messages et 10 commentaires pour chaque message sql

Quelque chose dans ce sens fonctionnerait.

with a as 
(
    SELECt TOP 10  Score
    , Post.ID as PostID 
    FROM Post 
    order by Score desc 
), b as
(
    select PostID
    , ID as CommentID 
    , ROW_NUMBER() over (partition by PostID order by ID) as RowNum
    from PostComment
) 
select * 
from a
left join b
on b.PostID = a.PostID
where b.RowNum <= 10