0

我想知道我的 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?有没有办法压缩这个查询以在一次运行中执行?

(如果您对语法、格式、良好实践等有任何辅助建议,请随时发表评论。此外,如果还有其他我没有考虑到的获得或失去声誉的方法(特定于标签),请发表评论.)

4

1 回答 1

1

你总是可以尝试类似的东西

SELECT
    SUM(
        CASE
            WHEN VoteTypeId = 2 AND Posts.PostTypeId = 1
                THEN 1
            ELSE 0
        END
    )   QuestionsUp,
    SUM(
        CASE
            WHEN VoteTypeId = 3 AND Posts.PostTypeId = 1
                THEN 1
            ELSE 0
        END
    )   QuestionsDown,
    SUM(
        CASE
            WHEN VoteTypeId = 2 AND Posts.PostTypeId = 2
                THEN 1
            ELSE 0
        END
    )   AnswersUp,
    SUM(
        CASE
            WHEN VoteTypeId = 3 AND Posts.PostTypeId = 2
                THEN 1
            ELSE 0
        END
    )   AnswersDown
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 
WHERE 
    Posts.OwnerUserId = @UserId and
    Tags.TagName = @Tag

编辑:

您可以使用 CTE,然后使用计算中的列。

就像是

;WITH Vals AS (
        SELECT
            SUM(
                CASE
                    WHEN VoteTypeId = 2 AND Posts.PostTypeId = 1
                        THEN 1
                    ELSE 0
                END
            )   QuestionsUp,
            SUM(
                CASE
                    WHEN VoteTypeId = 3 AND Posts.PostTypeId = 1
                        THEN 1
                    ELSE 0
                END
            )   QuestionsDown,
            SUM(
                CASE
                    WHEN VoteTypeId = 2 AND Posts.PostTypeId = 2
                        THEN 1
                    ELSE 0
                END
            )   AnswersUp,
            SUM(
                CASE
                    WHEN VoteTypeId = 3 AND Posts.PostTypeId = 2
                        THEN 1
                    ELSE 0
                END
            )   AnswersDown
        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 
        WHERE 
            Posts.OwnerUserId = @UserId and
            Tags.TagName = @Tag
        )
SELECT  QuestionsUp * 5 +
       AnswersUp * 10 +
       (QuestionsDown + AnswersDown) * -2
FROM    Vals
于 2013-11-01T06:09:20.537 回答