百分比高于Mysql中的数字

sd2nnvve  于 2023-02-15  发布在  Mysql
关注(0)|答案(1)|浏览(94)

我的表中有一列显示了客户排队等待服务的秒数,单位为秒。
我需要整理一个查询,显示有百分之多少的聊天是在30秒内完成的。
我只能计算出理想时间段内的通话次数,但无法计算出理想时间段内的聊天次数。

SELECT
    COUNT(c.id) AS 'Total'
FROM lh_chat as c
    LEFT JOIN lh_departament as d ON c.dep_id = d.id
    INNER JOIN lh_users u ON c.user_id = u.id
WHERE FROM_UNIXTIME(c.time-10800, '%Y-%m-%d') BETWEEN '2023-01-01' AND '2023-01-31'
    AND u.id != 178
    AND c.`status` <> 0
    AND c.dep_id IN (7,19,23)
    AND c.wait_time > 30

我只能添加超出理想时间的聊天次数,但无法计算预期范围内的聊天百分比

js4nwp54

js4nwp541#

你能试试这个吗:

SELECT SUM(IF(c.wait_time > 30,  0, 1) * 1.0 / COUNT(c.id)
FROM lh_chat as c
    LEFT JOIN lh_departament as d ON c.dep_id = d.id
    INNER JOIN lh_users u ON c.user_id = u.id
WHERE FROM_UNIXTIME(c.time-10800, '%Y-%m-%d') BETWEEN '2023-01-01' AND '2023-01-31'
    AND u.id != 178
    AND c.`status` <> 0

其思想是使用SUM只计算小于30秒的wait_times,然后除以总计数。AND c. dep_id IN(7,19,23)

相关问题