gpt4 book ai didi

MySQL GROUP BY 每个逗号分隔值

转载 作者:太空宇宙 更新时间:2023-11-03 12:04:27 26 4
gpt4 key购买 nike

在任何人评论之前,我没有用逗号分隔值设计这个数据库:)我花了很多时间试图找到答案,但我所能找到的只是 GROUP_CONCAT(),它似乎与我想要的相反。

我想对逗号分隔值字段中的每个值进行 GROUP BY。

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

期望输出:分配给字段分组的每个值的平均四舍五入数字。

编辑 - 尝试将其提升到一个新的水平:我想包含工单答案表以获得该工单的第一个响应,使用它的日期时间字段计算出每个用户的平均响应时间。

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

最佳答案

哎呀。假设您知道分配的最大数量,您可以这样做。这是一种方法:

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;

很抱歉您必须处理这种格式错误的数据库结构。

关于MySQL GROUP BY 每个逗号分隔值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27293178/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com