获取 mysql 列的百分比

Get percentage of mysql columns

我有一个 SQL 查询将输出以下内容:

+--------+-------------------+----------+---------+
| tagged | cat               | duration | minutes |
+--------+-------------------+----------+---------+
|     15 | Account           | 00:06:22 |      96 |
|      1 | Circuit           | 00:24:29 |      24 |
+--------+-------------------+----------+---------+

这是我的查询

$stmt = $db->prepare("SELECT count(DISTINCT nid) AS tagged,
       cat,
       SEC_TO_TIME(AVG(TIME_TO_SEC(duration))) AS duration,
       round(sum(time_to_sec(duration))/60, 0) AS minutes
FROM client_note
JOIN client_note_tag_items ON client_note_tag_items.note_id = client_note.nid
LEFT
JOIN client_note_tags ON client_note_tags.tag_id = client_note_tag_items.tag_id
WHERE dte >= ?
  AND dte <= ?
  AND name NOT LIKE 'Resolution%'
GROUP BY cat
ORDER
BY cat ASC");

我有另一个查询将在最后输出总计

SELECT count(DISTINCT nid) AS tagged,
       sec_to_time(avg(time_to_sec(duration))) AS TIME,
       round(sum(time_to_sec(duration))/60, 0) AS minutes
FROM client_note
JOIN client_note_tag_items ON client_note_tag_items.note_id = client_note.nid
LEFT
JOIN client_note_tags ON client_note_tags.tag_id = client_note_tag_items.tag_id
WHERE dte >= ?
  AND dte <= ?
  AND name NOT
LIKE 'Resolution%'
  AND duration IS NOT NULL;

总的来说,我想获得每个类别的总分钟数并计算他们使用的百分比。

+--------+-------------------+----------+---------+---------+
| tagged | cat               | duration | minutes | Percent |
+--------+-------------------+----------+---------+---------+
|     15 | Account           | 00:06:22 |      96 |   80%   |
|      1 | Circuit           | 00:24:29 |      24 |   20%   |
+--------+-------------------+----------+---------+---------+

我可以用 PHP 做这件事还是我可以用 mysql 做所有事情然后用 PHP 输出信息?

我想我可能已经解决了...

SELECT count(DISTINCT nid) AS tagged, cat,
 sec_to_time(avg(time_to_sec(duration))) AS time, 
round(sum(time_to_sec(duration))/60,0) AS minutes, 
FORMAT(round(sum(time_to_sec(duration))/60,0)/t.s * 100),2) AS Percent 
FROM client_note 
CROSS JOIN (SELECT DISTINCT nid AS n, cat AS c, round(sum(time_to_sec(duration))/60,0) AS s 
FROM client_note 
JOIN client_note_tag_items ON client_note_tag_items.note_id = client_note.nid 
LEFT JOIN client_note_tags ON client_note_tags.tag_id = client_note_tag_items.tag_id 
WHERE dte >= ?  AND dte <= ? AND name NOT LIKE 'Resolution%') t 
JOIN client_note_tag_items ON client_note_tag_items.note_id = client_note.nid 
LEFT JOIN client_note_tags ON client_note_tags.tag_id = client_note_tag_items.tag_id 
WHERE dte >= ? 
AND dte <= ? 
AND name NOT LIKE 'Resolution%' 
AND duration IS not null 
GROUP BY cat 
ORDER BY cat ASC;

这给了我

的输出
+--------+-------------------+----------+---------+---------+
| tagged | cat               | time     | minutes | Percent |
+--------+-------------------+----------+---------+---------+
|     15 | Account           | 00:05:30 |     96  | 1.80    |
|      1 | Circuit           | 00:11:39 |     24  | 0.45    |
....
+-----------------------------------------------------------+
16 rows total 

此时间范围内的总分钟数为 5344 .. 因此 (96/5344) * 100 = 1.796 或四舍五入为 1.80 帐户!