Table Parent
Pid Name
1 aa
2 bb
3 ddd
4 aaa
Table Child
Cid Pid Name
1 2 aa
2 1 bb
3 2 bb
4 3 cc
5 3 cd
Table Article
Aid Cid Context
1 3 asdfasdfasdf
2 2 asdfasdfasdfasf
3 3 asdfasdfasdfasdf
4 1 asdfasdfasdfasdf
想从每个Parent分类下相关的任意Child提取3条Article
WITH OrderedArticle AS
(
SELECT Pid, Aid, ROW_NUMBER() OVER (PARTITION BY Pid ORDER BY Aid) AS Num
FROM Parent
JOIN Child ON Parent.Pid = Child.Pid
JOIN Article ON Child.Cid = Article.Aid
)
SELECT *
FROM OrderedArticle
WHERE Num < 4