简体   繁体   English

SQL:SUM不能与COUNT一起使用,但可以单独使用

[英]SQL: SUM doesn't work with COUNT, but works well alone

I'm trying to execute the following SQL query in 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)";

These are the Tables 这些是表

Questions_Rating Questions_Rating

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

The rate can only be either 1 or -1. 速率只能是1或-1。

Questions 问题

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

Answers 答案

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

The query is returning these results 查询返回这些结果

SUM------COUNT
NULL       0

While it should return... 虽然它应该返回...

SUM------COUNT
1          0

Note that the following query returns the right result (1) 请注意,以下查询返回正确的结果(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) ";

I think you should consider rewriting your query to use JOINS instead of your subqueries: 我认为您应该考虑重写查询以使用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

See SQL Fiddle with Demo 参见带有演示的SQL Fiddle

Returns the result: 返回结果:

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

When you join the tables you're getting NULL values for r.rate. 当您加入表时,您将获得r.rate的NULL值。 Try using the ISNULL function to check for NULL values like this: 尝试使用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)";
AND q.best_answer_id IN (SELECT answer_id FROM answers WHERE user_id = 1)

You have no entries where user_id = 1 in the answers table, so this subquery will return no answer_ids. 您在答案表中没有user_id = 1条目,因此此子查询将不返回任何answer_ids。 You have effectively excluded all rows with this filter and your aggregates are properly calculating them as such. 您已使用此过滤器有效地排除了所有行,并且您的聚合正好正确地计算了它们。

The query that returned results did not have this line as part of the WHERE filter. 返回结果的查询没有此行作为WHERE筛选器的一部分。

The most likely explanation is that your query is not returning any rows. 最可能的解释是您的查询没有返回任何行。

I suggest you remove the aggregates, and check the results, eg: 我建议您删除聚合,并检查结果,例如:

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)

I believe you will find that there are no rows returned. 我相信您会发现没有返回任何行。 In that case, a COUNT() aggregate will reasonably return a 0, and a SUM() aggregate will return a NULL. 在这种情况下, COUNT()聚合将合理地返回0,而SUM()聚合将返回NULL。

It's fairly easy to modify the query to replace the NULL value with a zero, but in this case, I don't believe that is your issue. 修改查询以将NULL值替换为零是相当容易的,但是在这种情况下,我认为这不是您的问题。 (It's not at all clear why you would want a Cartesian product of the questions and questions_rating tables.) (并不清楚为什么要使用questionsquestions_rating表的笛卡尔积。)

I think your first step needs to be getting the rows returned that you need returned, and then work on getting the aggregates applied. 我认为您的第一步需要是获取需要返回的行,然后着手应用聚合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM