-1

如标题所述,我们希望获得原始提问者接受他们自己的答案作为最佳答案的所有帖子(包括用户、评论和建议的编辑)。换句话说,所有情况下OwnerUserId for PostTypeId 1 = OwnerUserId for PostTypeId 2

SEDE DB我正在为此苦苦挣扎,因为我对架构没有深入的了解。

这是来自SEDE获得具有高自我接受率(并且具有> 10个答案)的用户的示例,但我们需要具有自我接受答案的帖子,而不仅仅是用户。

SELECT TOP 100 
  Users.Id AS [User Link],
  (
    CAST(COUNT(a.Id) AS float) / 
    CAST(
      (
        SELECT COUNT(*) 
        FROM Posts p 
        WHERE p.OwnerUserId = Users.Id 
        AND PostTypeId = 1
      ) 
      AS float
    ) * 100
  ) AS SelfAnswerPercentage
FROM Posts q
  INNER JOIN Posts a ON q.AcceptedAnswerId = a.Id
  INNER JOIN Users ON Users.Id = q.OwnerUserId
WHERE q.OwnerUserId = a.OwnerUserId
GROUP BY Users.Id, DisplayName
HAVING COUNT(a.Id) > 10
ORDER BY SelfAnswerPercentage DESC
4

1 回答 1

0

你能发布你的预期输出吗?你的表的架构?

我不熟悉您的数据库架构,但这可能是一个起点:

SELECT q.*
FROM posts q -- Get all posts
INNER JOIN posts a ON q.AcceptedAnswerId = a.Id
  AND q.OwnerUserId = a.OwnerUserId AND a.PostTypeId = 2 -- Get accepted self-answers
INNER JOIN users u ON u.Id = q.OwnerUserId -- get OP's user info
WHERE q.PostTypeId = 1 -- Only get questions

我不确定你的comments&suggested edits存储在哪里,但我猜你需要做某种group_concat()或 JSON 类型的聚合来返回该数据,如果你只希望每个问题发布一行。

于 2019-09-22T08:31:03.810 回答