我有桌子:
+--------+------+------------+
| id | type | timestamp |
+--------+------+------------+
| 927061 | 573 | 1520284933 |
| 927062 | 573 | 1520284933 |
| 927063 | 573 | 1520284933 |
| 927064 | 573 | 1520284933 |
| 927347 | 573 | 1520285483 |
| 928796 | 573 | 1520287665 |
| 928799 | 573 | 1520287672 |
| 928801 | 573 | 1520287676 |
| 928802 | 573 | 1520287680 |
| 928806 | 573 | 1520287684 |
| 928821 | 573 | 1520287735 |
| 928822 | 573 | 1520287735 |
| 928823 | 573 | 1520287735 |
| 928824 | 573 | 1520287735 |
| 928825 | 573 | 1520287735 |
| 928826 | 573 | 1520287735 |
| 928827 | 573 | 1520287736 |
| 928828 | 573 | 1520287736 |
| 928829 | 573 | 1520287736 |
| 928830 | 573 | 1520287736 |
+--------+------+------------+
该表现在有4,134,798行并且计数...并且记录有318种类型并且正在计数。 我想根据时间戳创建每种类型的已用时间报告。每条记录都意味着某种类型的活动。
类似于的报告:
type - elapsed time
573 - 3 days 4 hours 3 minutes
103 - 1 days 1 hours 1 minutes
我必须遍历所有记录,添加某种类型事件之间的经过时间,存储某类事件的总耗用时间,并按类型显示已用时间。
有人愿意给我一些见解吗?
这是我的疑问:
SET @total = 0;
SET @curr_timestamp = 0;
SET @curr_id = 0;
SET @lineitem = 0;
SELECT CONCAT(FLOOR(HOUR(SEC_TO_TIME(Dedication)) / 24), ' días', ' ',TIME_FORMAT(SEC_TO_TIME(Dedication % (24 * 60 * 60)), '%H'), ' horas',' ',TIME_FORMAT(SEC_TO_TIME(Dedication % (24 * 60 * 60)), '%i'), ' minutos') As Dedicacion FROM
(
SELECT type,
(@lineitem:=@lineitem+1) line,
(@prev_id:=@curr_id),
(@curr_id:=id),
(@prev_timestamp:=@curr_timestamp),
(@curr_timestamp:=timestamp),
(@newvalue:=IF(@prev_id>@curr_id,@prev_timestamp+@curr_timestamp,ABS(@prev_timestamp-@curr_timestamp))) newval,
(@total:=@total+@newvalue) Dedication
FROM mdl_logstore_standard_log WHERE (DATEDIFF(NOW(),FROM_UNIXTIME(timestamp) ) < 7) AND type=573
) A WHERE line = @lineitem;
查询基于:
谢谢, 豪尔赫·达维拉。
答案 0 :(得分:1)
以下是如何完成所需内容的示例:
SELECT
type,
TIMESTAMPDIFF(YEAR, MIN(timestamp), MAX(timestamp)) AS in_years,
TIMESTAMPDIFF(MONTH, MIN(timestamp), MAX(timestamp)) AS in_month,
TIMESTAMPDIFF(DAY, MIN(timestamp), MAX(timestamp)) AS in_days,
TIMESTAMPDIFF(MINUTE, MIN(timestamp), MAX(timestamp)) AS in_minutes,
TIMESTAMPDIFF(SECOND, MIN(timestamp), MAX(timestamp)) AS in_seconds
FROM
activities
GROUP BY type
答案 1 :(得分:1)
您的时间戳似乎是Unix所代表的秒数。您可以在几秒钟内获得差异:
select type, max(timestamp) - min(timestamp) as diff_in_seconds
from activities
group by type;
将此转换为时间取决于您使用的数据库的功能。将其转换为十进制天或小时非常容易。例如:
select type, (max(timestamp) - min(timestamp)) / (60 * 60) as diff_hours
from activities
group by type;