【问题标题】:Fix time issue in oracle修复oracle中的时间问题
【发布时间】:2021-08-13 01:24:43
【问题描述】:

表格如下:

预期答案如下:

编写一个 oracle 查询来修复空间并返回 hh:mm 格式。考虑同一组中的同一时间并返回总计数

【问题讨论】:

标签: oracle time trim


【解决方案1】:

以下查询中的 WITH 子句仅用于测试;删除它,并在主查询中使用实际的表名和列名。注意:count 是保留关键字,所以不能是列名。我把它改成了count_(带下划线)。

with
  test_data (srt_tm, count_) as (
    select '1:00'  ,   125 from dual union all
    select '01:00' , 19000 from dual union all
    select ' 01:00',    27 from dual union all
    select '4:00'  , 22000 from dual union all
    select '04:00' ,  1800 from dual union all
    select ' 04:00', 15000 from dual
  )
-- END OF TEST DATA; ACTUAL QUERY BEGINS **BELOW THIS LINE**
select lpad(trim(srt_tm), 5, '0') as srt_tm, sum(count_) as count_
from   test_data
group  by lpad(trim(srt_tm), 5, '0')
order  by srt_tm
;

SRT_TM    COUNT_
------  --------
01:00      19152
04:00      38800

【讨论】:

  • 谢谢!!!是的,你是对的。我没有计数列。但需要根据每个组的 SRT_TM 计数
  • 我们可以在相同的情况下使用正则表达式选项吗?
  • 你可以,但你不应该。对于这样一个简单的问题,没有理由使用慢得多的正则表达式。
  • 如何在此处使用 regexp_replace 选项来解决同样的问题。问是,使用正则表达式选项。尝试以下选项 select (strt_tm,regexp_replace(strt_tm,'^([0-1]?[0-9]|2[0-3]):[0-5][0-9]$','hh: mm') 来自 test_data where strt_tm ='1:00'
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-11-29
  • 1970-01-01
  • 2016-10-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多