一天中sql查询的总时间计算,其中24小时格式的时间为hhmm

时间:2017-09-20 07:49:47

标签: sql oracle11g

我有一张表,其中包含日期(日期),剩余时间(varchar2(4))和到达时间(varchar2(4))。所用时间为24小时格式,如hhmm。如果一个人每天旅行3次,那么计算一天的总旅行时间的查询是什么? 我正在使用oracle 11g。请帮助。谢谢。

3 个答案:

答案 0 :(得分:0)

将值转换为数字并以分钟为单位报告:

select to_number(substring(time, 1, 2))*60 + to_number(substring(time, 3, 2)) as minutes

您的查询类似于:

select person, sum(to_number(substring(time, 1, 2))*60 + to_number(substring(time, 3, 2))) as minutes
from t
group by person;

我认为没有理由将其转换回字符串 - 或者甚至将值存储为字符串而不是数字。但是如果你需要,你可以反转这个过程来获得一个字符串。

答案 1 :(得分:0)

有两个答案,如果你只想在约会时间,那么可以这样做: -

select curr_date,
sum(24 * (to_date(arrival_time, 'HH24:mi:ss')- to_date(left_time, 'HH24:mi:ss'))) as difference 
from sql_prac group by curr_date,arrival_time,left_time;

示例输出如下: -

select curr_date,left_time,arrival_time from sql_prac;

CURR_DATE LEFT_TIME            ARRIVAL_TIME       
--------- -------------------- --------------------
30-JUN-17 00:00:00             15:00:00             
30-JUL-17 03:30:00             11:30:00             
30-AUG-17 03:00:00             12:30:00             
30-SEP-17 04:00:00             17:00:00             
30-JUN-17 00:00:00             15:00:00             
30-JUL-17 03:30:00             11:30:00             
30-AUG-17 03:00:00             12:30:00             
30-SEP-17 04:00:00             17:00:00             
30-SEP-17 04:00:00             17:00:00           

选择了9行

select curr_date,sum(24 * (to_date(arrival_time, 'HH24:mi:ss')- to_date(left_time, 'HH24:mi:ss'))) as difference 
from sql_prac group by curr_date,arrival_time,left_time;

CURR_DATE DIFFERENCE
--------- ----------
30-JUN-17         30 
30-JUL-17         16 
30-SEP-17         39 
30-AUG-17         19 

答案 2 :(得分:0)

如果您想按人和日期求和,则可以按以下方式进行: -

Auth::user()->role()->name;

示例输出如下: -

  1. 表格中的数据是: -

    从sql_prac中选择dept,curr_date,left_time,arrival_time;

  2.   

    DEPT CURR_DATE LEFT_TIME ARRIVAL_TIME

        select dept,curr_date,sum(24 * (to_date(arrival_time, 'HH24:mi:ss')- to_date(left_time, 'HH24:mi:ss'))) as difference 
    from sql_prac group by dept,curr_date,arrival_time,left_time order by Dept;
    
    1. 使用查询

      获取数据

      选择dept,curr_date,sum(24 *(to_date(arrival_time,' HH24:mi:ss') - to_date(left_time,' HH24:mi:ss')))差别 来自spt_prac group by dept,curr_date,arrival_time,left_time order by dept;

    2. -------------------- --------- -------------------- --------------------
      A                    30-SEP-17 04:00:00             17:00:00             
      B                    30-SEP-17 04:00:00             17:00:00             
      C                    30-AUG-17 03:00:00             12:30:00             
      D                    30-DEC-17 04:00:00             17:00:00             
      A                    30-SEP-17 04:00:00             17:00:00             
      B                    30-JUL-17 03:30:00             11:30:00             
      C                    30-AUG-17 03:00:00             12:30:00             
      D                    30-SEP-17 04:00:00             17:00:00             
      R                    30-SEP-17 04:00:00             17:00:00