统计stack exchange数据库中AnswerCount值相同的单条匹配记录数

Counting the number of single matching records with the same AnswerCount value in the stack exchange database

我正在 https://data.stackexchange.com/Whosebug/query/new

查询 Stack Exchange 数据库

计算与另一个post具有相同AnswerCount值的post总数。

这是我尝试导致错误的尝试Something unexpected went wrong while running your query. Don't worry, blame is already being assigned.

想法是在 Posts table 中的 post/record 与另一个 post/record 的 AnswerCount 值有一个匹配后增加计数器, 而不是用它自己的 AnswerCount 值计算匹配项。

Select Count(*)
From Posts as p1
Join Posts as p2
On p2.Id = {
            Select Top 1 Id
            From p2
            Where p1.AnswerCount = p2.AnswerCount
            And p1.Id <> p2.Id
            };

这是 Stack Exchange post 我用作参考:How to Join to first row

你的逻辑是正确的,但我会在这里使用 exists:

SELECT COUNT(*)
FROM Posts p1
WHERE EXISTS (SELECT 1 FROM Posts p2
              WHERE p2.Id <> p1.Id AND p2.AnswerCount = p1.AnswerCount);

用简单的英语阅读,上面的查询说要计算每条 post 条记录,我们可以找到一条不同的 post 条记录 相同 AnswerCount 值与 不同的 Id 值(暗示它是不同的记录)。

计算与另一个 post 具有相同 AnswerCount 值的 post 的总数。

你的逻辑太复杂了。与当前行答案计数相同的其他行数与此答案计数减1的行数完全相同。只有当计数等于1时,才没有其他答案,因此:

select sum(cnt)
from
 ( -- count per AnswerCount
   Select AnswerCount, Count(*) as cnt
   From Posts
   group by AnswerCount
   having count(*) > 1 -- remove unique AnswerCount
 ) as dt

或添加更多详细信息:

select sum(cnt) as answers
  ,sum(case when cnt > 1 then cnt end) as same_AnswerCount_as others
  ,sum(case when cnt = 1 then cnt end) as Unique_AnswerCount
  ,max(AnswerCount)
from
 ( 
   Select AnswerCount, Count(*) as cnt
   From Posts
   group by AnswerCount
 ) as dt

顺便说一句,即使是最简单的查询,Data Explorer 当前也会失败并显示此错误消息。