Sqlserver
 sql >> Base de Dados >  >> RDS >> Sqlserver

obter os 10 melhores posts e 10 comentários para cada post sql


Algo nesse sentido funcionaria.
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