我想知道我的 Stack Overflow 声誉,不包括因询问和回答正则表达式问题而获得或失去的所有声誉(——这对我来说就像“脏钱”!)
Stack Exchange 的数据资源管理器允许我计算这个。但是,我只使用专有数据库和 C++ 包装类进行编程,所以我对自己的 SQL 技能并不太自信。尽管如此,我还是试了一下,最终提出了一个问题,提供了我的答案:
-- (approximate) reputation gained/lost on a specified tag
-- only counts post upvotes and downvotes
DECLARE @UserId int = ##UserId##
DECLARE @QuestionsUp int = 0;
DECLARE @QuestionsDown int = 0;
DECLARE @AnswersUp int = 0;
DECLARE @AnswersDown int = 0;
DECLARE @Tag nvarchar(25) = 'regex';
SELECT
@QuestionsUp = COUNT(*)
FROM Tags
INNER JOIN PostTags ON PostTags.TagId = Tags.id
INNER JOIN Posts ON Posts.ParentId = PostTags.PostId
INNER JOIN Votes ON Votes.PostId = Posts.Id and VoteTypeId = 2
WHERE
Posts.OwnerUserId = @UserId and
Posts.PostTypeId = 1 and
Tags.TagName = @Tag
SELECT
@QuestionsDown = COUNT(*)
FROM Tags
INNER JOIN PostTags ON PostTags.TagId = Tags.id
INNER JOIN Posts ON Posts.ParentId = PostTags.PostId
INNER JOIN Votes ON Votes.PostId = Posts.Id and VoteTypeId = 3
WHERE
Posts.OwnerUserId = @UserId and
Posts.PostTypeId = 1 and
Tags.TagName = @Tag
SELECT
@AnswersUp = COUNT(*)
FROM Tags
INNER JOIN PostTags ON PostTags.TagId = Tags.id
INNER JOIN Posts ON Posts.ParentId = PostTags.PostId
INNER JOIN Votes ON Votes.PostId = Posts.Id and VoteTypeId = 2
WHERE
Posts.OwnerUserId = @UserId and
Posts.PostTypeId = 2 and
Tags.TagName = @Tag
SELECT
@AnswersDown = COUNT(*)
FROM Tags
INNER JOIN PostTags ON PostTags.TagId = Tags.id
INNER JOIN Posts ON Posts.ParentId = PostTags.PostId
INNER JOIN Votes ON Votes.PostId = Posts.Id and VoteTypeId = 3
WHERE
Posts.OwnerUserId = @UserId and
Posts.PostTypeId = 2 and
Tags.TagName = @Tag
SELECT @QuestionsUp * 5 +
@AnswersUp * 10 +
(@QuestionsDown + @AnswersDown) * -2
不过,这不可能是最好的。四个单独的查询只是将投票赞成的问题加权 5,赞成的答案加权 10,反对投票的问题和答案加权 -2?有没有办法压缩这个查询以在一次运行中执行?
(如果您对语法、格式、良好实践等有任何辅助建议,请随时发表评论。此外,如果还有其他我没有考虑到的获得或失去声誉的方法(特定于标签),请发表评论.)