0

の質問をしたり答えたりすることで得た、または失ったすべての評判を除いた、スタック オーバーフローの評判を知りたいと思いました(—私には「汚れたお金」のように感じます!)。

Stack Exchange のData Explorerを使用すると、これを計算できます。しかし、私はプロプライエタリ データベースと 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 回の実行で実行する方法はありますか?

(構文、フォーマット、グッド プラクティスなどに関する補助的なアドバイスがあれば、お気軽にコメントしてください。また、タグに固有の評判を得るまたは失う他の方法がある場合は、コメントしてください。 .)

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 に答える