请参阅SEDE 架构:
- 通过标签获取问题。
- 对表使用测试比对标签列
Tags
使用操作更好。LIKE
前者可以快 20 倍。
- 使用第 1 步中的问题列表来获得答案。
- 使用问题列表和答案列表来获得评论。
- 请注意,SEDE 提供了奇妙的魔法列,例如
[User Link]
.
把它们放在一起,这是一种方法:
WITH questsByTags AS (
SELECT DISTINCT
q.Id
, q.OwnerUserId
FROM Posts q
INNER JOIN PostTags pt ON q.Id = pt.PostId
INNER JOIN Tags t ON t.Id = pt.TagId
WHERE q.PostTypeId = 1 -- questions
AND (
t.TagName = 'perl6'
OR t.TagName = 'rakudo'
OR (
t.TagName = 'parrot'
AND EXISTS (
SELECT * FROM PostTags pt2
INNER JOIN Tags t2 ON t2.Id = pt2.TagId
WHERE q.Id = pt2.PostId AND t2.TagName = 'perl'
) ) )
),
answersByTags AS (
SELECT a.Id
, a.OwnerUserId
FROM Posts a
INNER JOIN questsByTags qbt ON qbt.Id = a.ParentId
),
commntsByTags AS (
SELECT c.Id
, c.UserId AS [OwnerUserId]
FROM Comments c
INNER JOIN (
SELECT Id FROM questsByTags
UNION ALL SELECT Id FROM answersByTags
) AS allPosts
ON allPosts.Id = c.PostId
),
allUsers AS (
SELECT OwnerUserId FROM questsByTags
UNION SELECT OwnerUserId FROM answersByTags
UNION SELECT OwnerUserId FROM commntsByTags
)
SELECT au.OwnerUserId AS [User Link]
, (SELECT Count (qbt.Id) FROM questsByTags qbt WHERE qbt.OwnerUserId = au.OwnerUserId) AS [Num Qsts]
, (SELECT Count (abt.Id) FROM answersByTags abt WHERE abt.OwnerUserId = au.OwnerUserId) AS [Num Ans]
, (SELECT Count (cbt.Id) FROM commntsByTags cbt WHERE cbt.OwnerUserId = au.OwnerUserId) AS [Num Cmmnts]
FROM allUsers au
WHERE au.OwnerUserId IS NOT NULL
ORDER BY [Num Qsts] DESC, [Num Ans] DESC, [Num Cmmnts] DESC
你可以在这个 SEDE 链接上看到它的实时运行。