mysql - SQL:设置 AVG() 时上一列为空

标签 mysql sql

好吧,我对 SQL 有点菜鸟。事实上非常如此,所以如果这是不言而喻的,我深表歉意。

我试图从数据库中找出 3 件事(该表是发送的每条消息的日志):

  • 总回复时间
  • 10 分钟内回复的总数
  • 平均回复时间

这是我的 SQL:

        SELECT
           *, SUM(case when tmp.reply_time <= 10 then 1 else 0 end) as under_10_mins,
           COUNT(tmp.reply_time) AS total_replies
        FROM
           (SELECT 
              TIMESTAMPDIFF(MINUTE, `date`, reply_date) as reply_time
           FROM
               tme_email_staff_reply sr
           JOIN 
               tme_user u 
           ON 
               u.id = sr.staff_id
           JOIN
               tme_email_message m 
           ON           
               m.id = sr.message_id
                   WHERE
               `reply_date` >= '2017-04-01 00:00:00'
           AND 
               `reply_date` < '2017-04-27 00:00:00'
           ) 
        AS tmp

哪些输出:

    | reply_time | under_10_mins | total_replies |
    |        106 |           165 |           375 |

现在,当我添加时:

        SELECT
           *, SUM(case when tmp.reply_time <= 10 then 1 else 0 end) as under_10_mins,
           COUNT(tmp.reply_time) AS total_replies
        FROM
           (SELECT 
              TIMESTAMPDIFF(MINUTE, `date`, reply_date) as reply_time,
              (AVG(TIMESTAMPDIFF(SECOND, `date`, reply_date))/60) AS average_reply_time
           FROM
               tme_email_staff_reply sr
           JOIN 
               tme_user u 
           ON 
               u.id = sr.staff_id
           JOIN
               tme_email_message m 
           ON           
               m.id = sr.message_id
                   WHERE
               `reply_date` >= '2017-04-01 00:00:00'
           AND 
               `reply_date` < '2017-04-27 00:00:00'
           ) 
        AS tmp

我的回答是:

    | reply_time | average_reply_time |under_10_mins | total_replies |
    |        106 |       149.08626667 |            0 |             1 |

如您所见,under_10_minstotal_replies 字段已更改。

链接表的架构:

tme_email_staff_reply:

    id |    staff_id |   message_id |            reply_date |
     1 | 234,221,001 | 15fg16d5dgw2 |   2017-04-01 09:34:16 | 

tme_user

    id |    username |   password |    email |   dob |   gender | 
    // data omited

tme_email_message

   id | thread_id    | From | To | subject | message |  message_id
   // data omited

谁能告诉我为什么会这样?以及如何修复它?

最佳答案

为什么会这样?

让我们看看AVG :

AVG([DISTINCT] expr)

Returns the average value of expr. The DISTINCT option can be used to return the average of the distinct values of expr.

If there are no matching rows, AVG() returns NULL.

文档位于 13.19.1 Aggregate (GROUP BY) Function Descriptions还说:

If you use a group function in a statement containing no GROUP BY clause, it is equivalent to grouping on all rows. For more information, see Section 13.19.3, “MySQL Handling of GROUP BY”.

这意味着在子查询中,您使用了 avg 而没有 group by,这将 avg 所有行,然后在子查询中返回一行.

如何解决?

我认为你应该将 avg 从子查询移动到外部查询:

SELECT
   SUM(case when tmp.reply_time <= 10 then 1 else 0 end) as under_10_mins,
   COUNT(tmp.reply_time) AS total_replies,
   AVG(average_reply_time) AS average_reply_time
FROM
   (SELECT 
      TIMESTAMPDIFF(MINUTE, `date`, reply_date) as reply_time,
      (TIMESTAMPDIFF(SECOND, `date`, reply_date))/60 AS average_reply_time
   FROM
       tme_email_staff_reply sr
   JOIN 
       tme_user u 
   ON 
       u.id = sr.staff_id
   JOIN
       tme_email_message m 
   ON           
       m.id = sr.message_id
           WHERE
       `reply_date` >= '2017-04-01 00:00:00'
   AND 
       `reply_date` < '2017-04-27 00:00:00'
   ) 
AS tmp

关于mysql - SQL:设置 AVG() 时上一列为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43651697/

相关文章:

mysql - SQL 查询 : What groups is a given member NOT a member?

php - MYSQL LIKE 匹配字符串的一部分

mysql - 带有 where 条件的 select 语句(选择全部或部分)

mysql - 返回 MySQL 中至少 2 个或更多 bool 列为 'true' 的记录

mysql - 带有子查询MySql

具有多个选择线程的 MySQL 生产者消费者

mysql - mysql内部连接出现sql错误

java,如果变量 "varConsult"的值位于表 "protocolo"的列 "pessoajuridica"中,如何查询 sql lite,

python - 删除/创建与删除/更新的性能

mysql - 在 MySQL 中,如何连接两个在 WHERE 条件下都有列的非常大的表?