SQL:SUM不适用于COUNT,但单独运行良好

时间:2013-01-09 21:33:22

标签: sql sql-server sql-server-2008

我正在尝试在SQL Server 2008中执行以下SQL查询

String query = "SELECT SUM(r.rate),COUNT(q.best_answer_id) " +
                            "FROM questions_rating r,questions q " +
                            "WHERE r.question_id IN (SELECT question_id FROM questions WHERE user_id = 1) "+
                            "AND q.best_answer_id IN (SELECT answer_id FROM answers WHERE user_id = 1)";

这些是表

Questions_Rating

id---------question_id-----------user_id--------rate
1               1                    1            1
2               1                    2            1
3               1                    3           -1
4               2                    1           -1

费率只能是1或-1。

问题

question_id------question-------user_id-------best_answer_id
1                   lala            1             3
2                   lala            2             5

答案

answer_id---------answer--------user_id------question_id
1                   lala            4             1
2                   kaka            5             1
3                   dada            6             1
4                   fafa            7             2
5                   tata            8             2

查询返回这些结果

SUM------COUNT
NULL       0

虽然应该回归......

SUM------COUNT
1          0

请注意,以下查询返回正确的结果(1)

String query = "SELECT SUM(r.rate) " +
                            "FROM questions_rating r " +
                            "WHERE r.question_id IN (SELECT question_id FROM questions WHERE user_id = 1) ";

4 个答案:

答案 0 :(得分:3)

我认为你应该考虑重写你的查询以使用JOINS而不是你的子查询:

SELECT 
  SUM(r.rate) SumOfRate, 
  COUNT(a.answer_id) CountOfBest
FROM questions_rating r
INNER JOIN questions q
  ON r.question_id = q.question_id
  AND r.user_id = q.user_id
LEFT JOIN answers a
  ON q.best_answer_id = a.answer_id
  AND a.user_id = 1
WHERE r.user_id = 1

请参阅SQL Fiddle with Demo

返回结果:

| SUMOFRATE | COUNTOFBEST |
---------------------------
|         1 |           0 |

答案 1 :(得分:1)

当您加入表时,您将获得r.rate的NULL值。尝试使用ISNULL函数检查NULL值,如下所示:

String query = "SELECT SUM(ISNULL(r.rate,0)),COUNT(ISNULL(q.best_answer_id),0)) " +
                            "FROM questions_rating r,questions q " +
                            "WHERE r.question_id IN (SELECT question_id FROM questions WHERE user_id = 1) "+
                            "AND q.best_answer_id IN (SELECT answer_id FROM answers WHERE user_id = 1)";

答案 2 :(得分:1)

AND q.best_answer_id IN (SELECT answer_id FROM answers WHERE user_id = 1)

答案表中没有user_id = 1的条目,因此该子查询将不返回任何answer_ids。您已使用此过滤器有效地排除了所有行,并且您的聚合正确地计算它们。

返回结果的查询没有将此行作为WHERE过滤器的一部分。

答案 3 :(得分:1)

最可能的解释是您的查询没有返回任何行。

我建议您删除聚合,并检查结果,例如:

SELECT r.rate
     , q.best_answer_id
  FROM questions_rating r
 CROSS
  JOIN questions q
 WHERE r.question_id IN
       (SELECT question_id FROM questions WHERE user_id = 1)
   AND q.best_answer_id IN
       (SELECT answer_id FROM answers WHERE user_id = 1)

我相信你会发现没有返回任何行。在这种情况下,COUNT()聚合将合理地返回0,而SUM()聚合将返回NULL。

修改查询以用零替换NULL值相当容易,但在这种情况下,我不相信这是你的问题。 (完全不清楚为什么你想要questionsquestions_rating表的笛卡尔积。)

我认为您的第一步需要获取您需要返回的返回行,然后继续应用聚合。