sql >> Base de Datos >  >> RDS >> Sqlserver

obtenga las 10 publicaciones principales y los 10 comentarios para cada publicación sql

Algo así funcionaría.

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