2列查询的差异

时间:2014-04-06 14:24:14

标签: sql postgresql select difference

我有VoteQuestion表:VoteQuestion(idUser → User, idQuestion → Content, isUp)

Question表:Question(idQuestion → Content, title)

我想选择得分最高的5个问题(upvotes-downvotes) upvote是VoteQuestion isUp = TRUEVoteQuestion isUp = 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 个答案:

答案 0 :(得分:2)

使用内联条件语句将TrueFalse值转换为数字(分别为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

答案 1 :(得分: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

希望它可以帮到你

<强>更新

我们试试这个:

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