2014-04-06 129 views
0

的2列的我有一個VoteQuestion表:VoteQuestion(idUser → User, idQuestion → Content, isUp)差異查詢

Question表:Question(idQuestion → Content, title)

我要選擇的5個問題的最佳成績(upvotes-downvotes)。
向上投票是VoteQuestionisUp = TRUE和downvote a VoteQuestionisUp = FALSE

我嘗試了以下查詢,但這不是我正在尋找的。它爲每個問題提供相同的分數。

WITH upvotes AS 
( 
    SELECT COUNT(*) AS "a" 
    FROM "VoteQuestion", "Question" 
    WHERE "VoteQuestion"."idQuestion" = "Question"."idQuestion" 
    AND "VoteQuestion"."isUp" IS TRUE 
), 
downvotes AS 
( 
    SELECT COUNT(*) AS "b" 
    FROM "VoteQuestion", "Question" 
    WHERE "VoteQuestion"."idQuestion" = "Question"."idQuestion" 
    AND "VoteQuestion"."isUp" IS FALSE 
) 
SELECT "title", "Question"."idQuestion", upvotes."a"-downvotes."b" AS total 
FROM "VoteQuestion", "Question", upvotes, downvotes 
WHERE "VoteQuestion"."idQuestion" = "Question"."idQuestion" 
GROUP BY "title", "Question"."idQuestion", upvotes."a", downvotes."b" 
ORDER BY "total" DESC 
LIMIT 5 

也試過這樣:

SELECT "title", "Question"."idQuestion", 
(SELECT COUNT(*) 
    FROM "VoteQuestion", "Question" 
    WHERE "VoteQuestion"."idQuestion" = "Question"."idQuestion" 
    AND "VoteQuestion"."isUp" IS TRUE 
) AS upvotes, 
(SELECT COUNT(*) 
    FROM "VoteQuestion", "Question" 
    WHERE "VoteQuestion"."idQuestion" = "Question"."idQuestion" 
    AND "VoteQuestion"."isUp" IS FALSE 
) AS downvotes, upvotes-downvotes AS total 
FROM "VoteQuestion", "Question" 
WHERE "VoteQuestion"."idQuestion" = "Question"."idQuestion" 
GROUP BY "title", "Question"."idQuestion" 
ORDER BY total DESC 
LIMIT 5 

但它說ERROR: column "upvotes" does not exist

我怎樣才能得到來自查詢的列upvotes和downvotes的區別?

回答

2

隨着條件語句內嵌到您的True轉換和(分別爲1和-1)爲數字,你可以簡單地總結一下票是這樣的:

SELECT 
    q."title", 
    q."idQuestion", 
    sum(
     case 
      when v."isUp" then 1 
      else -1 
     end 
    ) AS total 

FROM 
    VoteQuestion v join Question q on v."idQuestion" = q."idQuestion" 

GROUP BY 
    q."title", 
    q."idQuestion" 

ORDER BY 
    3 DESC 

LIMIT 5 

我還沒有嘗試過,但我敢肯定,這個工程。

編輯: 下面是其成功運行的SQLFiddle:http://sqlfiddle.com/#!15/df4ca/3

+0

是的,試過這個,它的工作原理。順便說一句,你有一個錯字 - 'ORDER BY total DESC'。感謝您的回答,謝謝! –

+0

'ORDER BY 3 DESC'指的是列總數,因爲它是投影中的第三列。 –

+0

哦,一直在學習! :) –

0

試試這個:

SELECT "title", "Question"."idQuestion", 
(SELECT COUNT(*) as upvotes 
    FROM "VoteQuestion", "Question" 
    WHERE "VoteQuestion"."idQuestion" = "Question"."idQuestion" 
    AND "VoteQuestion"."isUp" IS TRUE 
) AS upvt, 
(SELECT COUNT(*) as downvotes 
    FROM "VoteQuestion", "Question" 
    WHERE "VoteQuestion"."idQuestion" = "Question"."idQuestion" 
    AND "VoteQuestion"."isUp" IS FALSE 
) AS downvt, upvt.upvotes-downvt.downvotes AS total 
FROM "VoteQuestion", "Question" 
WHERE "VoteQuestion"."idQuestion" = "Question"."idQuestion" 
GROUP BY "title", "Question"."idQuestion" 
ORDER BY total DESC 
LIMIT 5 

希望它可以幫助你

UPDATE

讓我們試試這個:

SELECT "title", "Question"."idQuestion", 
upvotes, downvotes, upvotes-downvotes AS total 
FROM "VoteQuestion", "Question", 
(SELECT "idQuestion", COUNT(*) as upvotes 
    FROM "VoteQuestion", "Question" 
    WHERE "VoteQuestion"."idQuestion" = "Question"."idQuestion" 
    AND "VoteQuestion"."isUp" IS TRUE GROUP BY "idQuestion" 
) AS upvt, 
(SELECT "idQuestion", COUNT(*) as downvotes 
    FROM "VoteQuestion", "Question" 
    WHERE "VoteQuestion"."idQuestion" = "Question"."idQuestion" 
    AND "VoteQuestion"."isUp" IS FALSE GROUP BY "idQuestion" 
) AS downvt 
WHERE "VoteQuestion"."idQuestion" = "Question"."idQuestion" 
AND "VoteQuestion"."idQuestion" = downvt."idQuestion" 
AND "VoteQuestion"."idQuestion" = upvt."idQuestion" 
GROUP BY "title", "Question"."idQuestion" 
ORDER BY total DESC 
LIMIT 5 
+0

感謝您的回答。在來這裏之前,我嘗試過類似的東西。實際上,它表示「缺少表upvt的FROM子句條目」。然後我在'FROM'子句中添加'upvt',它說'關係'upvt「不存在'。你知道什麼可能是錯的嗎? –

+0

分別測試每個部分。首先執行此操作:SELECT COUNT(*)as upvotes FROM「VoteQuestion」,「Question」 WHERE「VoteQuestion」。「idQuestion」=「Question」。「idQuestion」 AND「VoteQuestion」。「isUp」IS TRUE 它給你upvotes? – ericpap

+0

缺少那裏的條件 - 「AND」VoteQuestion「。」idQuestion「=」問題「。」idQuestion「。除此之外,是的,它讓我讚不絕口。 –