如何避免GROUP_CONCAT()行乘以我的SUM()

时间:2018-06-26 06:56:59

标签: mysql sql

因此,我有一个查询,该查询可检索支持票证,并通过GROUP_CONCAT()即时检索一行中的每个票证状态,以PHP数组的形式进行处理。

我的问题是,在查询中,我有一个SUM()可以从他的干预中获取每张故障单的时间,但是,例如,如果GROUP_CONCAT()检索到12个状态并且故障单总和为2400秒, SUM()的最终结果是2400 x 12 = 28800。

这是我的查询:

SELECT t.subject as theme, SUM(fd.duree) as time, t.datec, t.date_close, t.category_code as category, GROUP_CONCAT(DISTINCT IFNULL(tl.status, 0),'_',tl.datec ORDER BY tl.datec) as status
FROM llx_ticketsup as t
JOIN llx_societe as s on s.rowid = t.fk_soc
JOIN llx_user as u on u.rowid = t.fk_user_assign
JOIN llx_element_element as ee on ee.fk_source = t.rowid
JOIN llx_fichinter as f on f.rowid = ee.fk_target
JOIN llx_fichinterdet as fd on fd.fk_fichinter = f.rowid
JOIN llx_ticketsup_logs as tl on tl.fk_track_id = t.track_id
WHERE t.fk_statut = 8
AND t.fk_soc = 165
AND (STR_TO_DATE(t.date_close, '%Y-%m-%d') BETWEEN '2018-06-25' AND '2018-06-25 23:59:59')
GROUP BY t.rowid

结果:

enter image description here

应该准时到达2400,但要乘以他的12个状态。

如果我也按状态进行分组,那么时间就如您所见,但是我只需要一行包含票证实时支出和他的状态即可。

enter image description here

我的问题是,如何避免GROUP_CONCAT()行不乘以我的SUM()

* EDIT :我通过除以SUM(fd.duree)/COUNT(DISTINCT tl.rowid)使其起作用。我知道这很奇怪,但是不知道该怎么做。如果有人有任何建议,将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:0)

SELECT t.subject as theme, 
  SUM(fd.duree) as time, 
  t.datec, t.date_close, 
  t.category_code as category, 
  (SELECT GROUP_CONCAT(DISTINCT COALESCE(l.status, 0),'_',l.datec ORDER BY l.datec) 
    FROM llx_ticketsup_logs AS l WHERE fk_track_id = t.track_id) as status
FROM llx_ticketsup as t
JOIN llx_element_element as ee on ee.fk_source = t.rowid
JOIN llx_fichinter as f on f.rowid = ee.fk_target
JOIN llx_fichinterdet as fd on fd.fk_fichinter = f.rowid
WHERE t.fk_statut = 8
AND t.fk_soc = 165
AND (STR_TO_DATE(t.date_close, '%Y-%m-%d') BETWEEN '2018-06-25' AND '2018-06-25 23:59:59')
GROUP BY t.rowid