oracle - 从时间中提取小时
问题描述
我试图找到解决方案,但我做不到。问题如下。
1 ) I want to extract hours from time and add minutes converted to hours
(SUM(SUBSTRING_INDEX(aa.Quantity, ':', 1)) + TRUNCATE((SUM(SUBSTRING_INDEX(aa.Quantity, ':', -1)) / 60),0))
例如,如果我有 16 小时:35 分钟。我想显示 16 和分钟部分应该添加到小时例如 16,5 小时
2) extract minutes from time and find reminder (modulo)
LPAD((SUM(SUBSTRING_INDEX(aa.Quantity, ':', -1)) % 60), 2, 0)
我找到了这部分解决方案,但是这个解决方案是用 MySQL 编写的,我需要 Oracle SQL 解决方案
CONCAT(
-- extract hours froAm time and add minutes converted to hours
(SUM(SUBSTRING_INDEX(aa.Quantity, ':', 1)) + TRUNCATE((SUM(SUBSTRING_INDEX(aa.Quantity, ':', -1)) / 60),0))
, ':',
-- extract minutes from time and find reminder (modulo)*/
LPAD((SUM(SUBSTRING_INDEX(aa.Quantity, ':', -1)) % 60), 2, 0)
) AS W_TOTAL_SUM
此外,我尝试使用以下站点将此 MySQL 语句转换为 Oracle SQL,但不幸的是我没有得到正确的结果,因为它返回的结果output
与input
http://www.sqllines.com/online
与我描述的完全相同,但仅在 Oracle SQL 中。我会非常感激,因为我尝试解决这个问题几个小时并且找不到任何解决这个问题的方法
这是我的解决方案,它不起作用。我收到错误
ORA-00907: missing right parenthesis
SELECT
(SUM(SUBSTR(A.Quantity, ':', 1)) + TRUNC((SUM(SUBSTR(A.Quantity, ':', -1)) / 60),0)), ':' ,
MOD(LPAD(SUM(SUBSTR(A.Quantity, ':', -1)), 60),2,0)
-- MOD(LPAD((SUM(SUBSTRING_INDEX(A.Quantity, ':', -1)) % 60), 2, 0)
-- LPAD((SUM(SUBSTRING_INDEX(aa.Quantity, ':', -1)) % 60), 2, 0)
AS TOTAL_SUM FROM (
SELECT
ata.ATAID AS AtaId, ata.ProjectID, ata.StartDate, ataAW.Quantity
FROM
ata
INNER JOIN
weekly_report
ON
weekly_report.ataId = ata.ATAID
INNER JOIN
ata_articles ataAW
ON
ataAW.wrId = weekly_report.id
WHERE
ata.ATAID = 10987
AND
ataAW.type = 1
OR
ataAW.type = 2
OR
ataAW.type = 3
AND
(weekly_report.status != 3 AND weekly_report.status != 4)
AND
(
weekly_report.year < (SELECT year FROM weekly_report WHERE id = 89)
OR
(
weekly_report.year <= (SELECT year FROM weekly_report WHERE id = 89)
AND
weekly_report.week <= (SELECT week FROM weekly_report WHERE id = 89)
)
)
) A
group by A.AtaId
order by A.AtaId ASC
常见的问题是
LPAD((SUM(SUBSTR(A.Quantity, ':', -1)) MOD 60), 2, 0)
这是我期望的输出
TOTAL_SUM
101:24
解决方案
我是这样理解这个问题的:
- 第 1 - 2 行中的样本数据
- 提取小时数(第 3 行)
- 提取分钟(第 5 行),除以 60(一小时的分钟数)
- 小时 + 分钟 = 结果
SQL> with test (col) as
2 (select '16hours:35minutes' from dual)
3 select to_number(regexp_substr(col, '\d+', 1, 1)) -- hours
4 +
5 round(to_number(regexp_substr(col, '\d+', 1, 2)) / 60, 2) -- minutes
6 as result
7 from test
8 /
RESULT
----------
16,58
SQL>
推荐阅读
- swift - 刷新控制器中的声音
- python - Python 类对象如何存储在内存中?
- java - mongodb迁移导致key重复
- java - 使用Java从另一个postgres服务器向postgresql服务器插入更多一百万行的有效方法是什么?
- html - Pandoc 将文件渲染到同一个文件夹。我可以更改目标文件夹吗?
- amazon-web-services - AMI 列表的 Cloudformation 参数
- python - Python将变量放在文本中
- amazon-web-services - 一个 AWS 账户用于支付,另一个用于维护?
- ruby-on-rails - 如何将时间列表步骤 30 分钟和 60 分钟更改为 Ruby on Rails 中 time_field 的下拉列表?
- python - 在循环中格式化/对齐文本