在有人评论之前,我没有用逗号分隔值设计这个数据库:) 我花时间试图找到答案,但我能找到的只是GROUP_CONCAT(),这似乎与我想要的相反。
我想在逗号分隔值字段中为每个值分组。
SELECT round(avg(DATEDIFF( dateClosed , dateAded ) * 1.0), 2) AS avg, department
FROM tickets GROUP BY assignedto
assignedto字段是逗号分隔值字段
row1 54,69,555
row2 54,75,555
row3 75,555
DESIRED OUTPUT:分配给字段中每个值的平均舍入数字。
编辑 - 尝试将其带到下一级: 我想要包含故障单答案表以获取该故障单的第一个响应,使用其日期时间字段计算每个用户的平均响应时间。
SELECT a.id as theuser, round(avg(DATEDIFF( ta.dateAded , t.dateAded ) * 1.0), 2) as avg
FROM tickets t join
mdl_user a
on find_in_set(a.id, t.assignedto) > 0
INNER JOIN (SELECT MIN(ta.dateAded) as started FROM ticketanswer GROUP BY ta.ticketId) ta ON t.id = ta.ticketId
GROUP BY a.id ORDER BY avg ASC
答案 0 :(得分:2)
呸。假设您知道最大分配数,则可以执行此操作。这是一种方法:
select substring_index(substring_index(assignedto, ',', n.n), ',', -1) as assignedto,
round(avg(DATEDIFF( dateClosed , dateAded ) * 1.0), 2) as avg
from tickets t join
(select 1 as n union all select 2 union all select 3)
on length(assignedto) - length(replace(assignedto, ',', '')) < n.n
group by substring_index(substring_index(assignedto, ',', n.n), ',', -1);
或者,如果您有一个已分配值的列表,比如在AssignedTo
表中,则更简单:
select a.assignedto, round(avg(DATEDIFF( dateClosed , dateAded ) * 1.0), 2) as avg
from tickets t join
assignedto a
on find_in_set(a.assignedto, t.assignedto) > 0
group by a.assignedto;
对不起,您必须处理这种格式错误的数据库结构。