为什么这个 votes/posts 比率总是 1?

Why is this votes/posts ratio always 1?

我正在与 SEDE 合作创建投票与帖子比率的图表。消除了所有实际错误后,我面临一个新问题:由于某种原因,比率总是 1。这是当前的 SQL:

SELECT CAST(p.CreationDate AS DATE) AS [CreationDate],
       COUNT(CAST(v.CreationDate AS DATE)) / COUNT(CAST(p.CreationDate AS DATE))
         AS [Ratio]
FROM Posts p
INNER JOIN Votes v ON v.PostId = p.Id
WHERE v.VoteTypeId = ##VoteType:int?2## AND
      p.PostTypeId = 1 OR p.PostTypeId = 2
GROUP BY CAST(p.CreationDate AS DATE)
ORDER BY Ratio

可以找到查询本身 here

suggested in chat 这可能是因为加入表格会产生所有可能的组合,因此投票数和帖子数始终相同(因此 n/n = 1)。这是正确的吗?如果是这样,我应该怎么做?

由于内部联接中的两边都存在,COUNT(CAST(v.CreationDate AS DATE))COUNT(CAST(p.CreationDate AS DATE))将return完全相同的数字,即组中的行数*.

如果您想计算在给定日期每个新 post 有多少新选票,请使用 COUNT(DISTINCT):

SELECT CAST(p.CreationDate AS DATE) AS [CreationDate],
       COUNT(DISTINCT v.Id) / COUNT(DISTINCT p.Id) AS [Ratio]
FROM Posts p
INNER JOIN Votes v ON v.PostId = p.Id
WHERE v.VoteTypeId = ##VoteType:int?2## AND
      p.PostTypeId = 1 OR p.PostTypeId = 2
GROUP BY CAST(p.CreationDate AS DATE)
ORDER BY Ratio

* 假设 CreationDate 不可为空。