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

查询本身可以在这里找到。

聊天中建议这可能是因为加入表格会导致所有可能的组合,因此投票和帖子的数量总是相同的(因此n/n = 1)。这是正确的,如果是这样,我应该怎么做?

4

1 回答 1

2

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

如果您想计算在给定日期每个新帖子有多少新选票,请使用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不可为空。

于 2015-07-13T14:08:35.557 回答