MySQL SUM总分钟激活

时间:2018-06-11 16:02:56

标签: mysql

我需要计算某个状态被激活的总分钟数之和。

[实施例] [1]

id     disp_id  state   created_at
764912  2         1       11/06/2018 12:03
765261  2         1       11/06/2018 12:08
765401  2         1       11/06/2018 12:10
765540  2         1       11/06/2018 12:12
765610  2         1       11/06/2018 12:13
765889  2         1       11/06/2018 12:17
766029  2         1       11/06/2018 12:19
766098  2         1       11/06/2018 12:20
766168  2         1       11/06/2018 12:21
766656  2         1       11/06/2018 12:28

我需要这笔钱:

03->08 = 5 minutes
08->10 = 2 minutes
10->12 = 2 minutes
12->13 = 1 minutes
13->17 = 4 minutes
17->19 = 2 minutes
19->20 = 1 minutes
20->21 = 1 minutes
21->28 = 7 minutes

总计:25分钟

我试过(SUM(分钟(a.created_at)))但我有分钟的总和

03 + 08 + 10 + 12 + 13 + 17 + 19 + 20 + 21 + 28 = 151

是否可以使用sum执行此SQL?

谢谢你!

1 个答案:

答案 0 :(得分:1)

请参阅以下查询。

SELECT CAST((UNIX_TIMESTAMP(MAX(created_at)) - UNIX_TIMESTAMP(MIN(created_at)))/60 as UNSIGNED)  AS 'minutes';

示例

SELECT CAST((UNIX_TIMESTAMP('2018-06-11 12:28:00') - UNIX_TIMESTAMP('2018-06-11 12:03:00'))/60 as UNSIGNED)  AS 'minutes';
希望解决方案能为您提供帮助。