2013-11-01 35 views
0

我想知道我的堆棧溢出的聲譽排除所有獲得或失去從提問和回答問題(—感覺「髒錢」給我!)有沒有一種方法可以根據WHERE表達式來計算加權總和?

堆棧交易所Data Explorer,讓我來計算的口碑這個。但是,我只能使用專有數據庫和C++包裝類進行編程,所以我對自己的SQL技能並不太自信。儘管如此,我給它一個鏡頭,並最終想出了a query that provided my answer

-- (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個問題解答問題和答案?有沒有辦法壓縮這個查詢來執行一次運行?

(請隨時免費的,如果你有關於語法,格式,好的做法,這樣的任何輔助的建議發表評論。另外,請不要評論,如果有其他的方法來獲得或失去特有的標籤—我的天堂美譽— 「噸因素)

回答

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 
+0

哦,太好了!有沒有一種方法可以完全消除QuestionsUp,QuestionsDown,AnswersUp,AnswersDown變量,並在'SELECT'中執行整個計算? (我只需要最後的加權分數的總和。)我試着用相應的分數(5,-2,10,-2)替換'THEN 1'並將所有目標設置爲單個變量—,但是這個只爲我返回4列(全部使用單個變量的名稱),沒有結果。 –

相關問題