这是用于计算我在输入中添加的 hour_diff 的查询
SELECT t.START_TIME, t.END_TIME, (TIME_TO_SEC(t.END_TIME) - TIME_TO_SEC(t.START_TIME))/(60*60) as hour_diff FROM table1 as t ;
输入
start time --- end time ---- hour_diff
10:20:00 10:40:00 .33 hour
11:00:00 11:15:00 .25 hour
11:00:00 12:20:00 .33 hours
11:00:00 13:00:00 2 hours
14:00:00 17:00:00 3 hours
14:20:00 18:00:00 3.66 hours
输出 我的输出应该归类为
0th hour - 1st hour --> x1 rows
1st hour - 2nd hour ---> x2 rows
像这样:
0-1 ----> 3
1-2 ----> 1
2-3 ----> 1
3-4 ----> 1
4-5 ----> 0
有人可以建议我在表中有开始时间和结束时间,现在是小时数,这就是为什么我将其包括在查询中,但我不知道如何按小时将其分类。
答案 0 :(得分:2)
您可以尝试按hour_diff分组,并计算结果数量:
SELECT truncate((TIME_TO_SEC(t.END_TIME) - TIME_TO_SEC(t.START_TIME))/(60*60),0) as hour_diff, count(*)
FROM table1 as t
group by truncate((TIME_TO_SEC(t.END_TIME) - TIME_TO_SEC(t.START_TIME))/(60*60),0);