英文:
Formula to use to convert the hours entered to 1/4th hour
问题
我正在创建一个Oracle快速公式,在其中我试图将员工输入的时间四舍五入到最近的1/4小时。
15-22分钟 = 0.25小时
23-37分钟 = 0.50小时
38-52分钟 = 0.75小时
53分钟到1小时7分钟 = 1小时
我能够获得输入的小时数。
例如 - 输入时间 - 下午5点 - 下午8.25点将是3.42小时。
现在这应该转换为最近的1/4小时 - 3.75。
要将这个转换为3.42小时,我应该使用什么计算公式?
英文:
I am creating a Oracle fast formula where I am trying to round the time entered by employee to the nearest 1/4 hour.
15-22 minutes = 0.25 hours
23- 37 minutes = 0.50 hours
38-52 minutes = 0.75 hours
53 to 1 hr 7 mins = 1 hour
I am able to get the hours entered.
For Eg - Time entered - 5 PM - 8.25 PM will be 3.42 hours entered
Now this should be converted to the nearest 1/4 hour - 3.75.
what calculation formula should I use to convert this to 3.42 hours ?
答案1
得分: 2
通过使用Oracle的DATE
差异进行一些算术运算。计算1
表示一天,1 / (24 * 4)
表示一个刻钟,您可以计算
with dt as (
select date'2023-01-01' + rownum/(24*60) date_dt from dual
connect by level <= 24*60 - 1)
select date_dt,
round((date_dt - trunc(date_dt)) * 24 * 4) closest_qh,
trunc(date_dt) + round((date_dt - trunc(date_dt)) * 24 * 4) / (24 * 4) rounded_date,
round((date_dt - trunc(date_dt)) * 24 * 4) / 4 formula
from dt;
输出(为简洁起见删减)
DATE_DT CLOSEST_QH ROUNDED_DATE FORMULA
------------------- ---------- ------------------- ----------
...
01.01.2023 03:21:00 13 01.01.2023 03:15:00 3,25
01.01.2023 03:22:00 13 01.01.2023 03:15:00 3,25
01.01.2023 03:23:00 14 01.01.2023 03:30:00 3,5
01.01.2023 03:24:00 14 01.01.2023 03:30:00 3,5
...
01.01.2023 03:36:00 14 01.01.2023 03:30:00 3,5
01.01.2023 03:37:00 14 01.01.2023 03:30:00 3,5
01.01.2023 03:38:00 15 01.01.2023 03:45:00 3,75
01.01.2023 03:39:00 15 01.01.2023 03:45:00 3,75
...
01.01.2023 03:51:00 15 01.01.2023 03:45:00 3,75
01.01.2023 03:52:00 15 01.01.2023 03:45:00 3,75
01.01.2023 03:53:00 16 01.01.2023 04:00:00 4
01.01.2023 03:54:00 16 01.01.2023 04:00:00 4
英文:
With a bit of arithmetic using the Oracle DATE
differenz. Calulation 1
is one day, 1 / (24 * 4)
is a quartel of an hour, you may calculate
with dt as (
select date'2023-01-01' + rownum/(24*60) date_dt from dual
connect by level <= 24*60 - 1)
select date_dt,
round((date_dt - trunc(date_dt)) * 24 * 4) closest_qh,
trunc(date_dt) + round((date_dt - trunc(date_dt)) * 24 * 4) / (24 * 4) rounded_date,
round((date_dt - trunc(date_dt)) * 24 * 4) / 4 formula
from dt;
output (cut for brevity)
DATE_DT CLOSEST_QH ROUNDED_DATE FORMULA
------------------- ---------- ------------------- ----------
...
01.01.2023 03:21:00 13 01.01.2023 03:15:00 3,25
01.01.2023 03:22:00 13 01.01.2023 03:15:00 3,25
01.01.2023 03:23:00 14 01.01.2023 03:30:00 3,5
01.01.2023 03:24:00 14 01.01.2023 03:30:00 3,5
...
01.01.2023 03:36:00 14 01.01.2023 03:30:00 3,5
01.01.2023 03:37:00 14 01.01.2023 03:30:00 3,5
01.01.2023 03:38:00 15 01.01.2023 03:45:00 3,75
01.01.2023 03:39:00 15 01.01.2023 03:45:00 3,75
...
01.01.2023 03:51:00 15 01.01.2023 03:45:00 3,75
01.01.2023 03:52:00 15 01.01.2023 03:45:00 3,75
01.01.2023 03:53:00 16 01.01.2023 04:00:00 4
01.01.2023 03:54:00 16 01.01.2023 04:00:00 4
答案2
得分: 1
你可以使用TRUNC
将时间截断到小时的起始,然后将分钟和秒钟部分四舍五入到最近的15分钟,并添加:
SELECT time,
TRUNC(time, 'HH') + ROUND((time - TRUNC(time, 'HH'))*4*24)/4/24 AS rounded_time
FROM times
对于示例数据:
CREATE TABLE times (time) AS
SELECT TRUNC(SYSDATE, 'HH') + NUMTODSINTERVAL(LEVEL, 'MINUTE')
FROM DUAL
CONNECT BY LEVEL <= 60;
输出:
TIME | ROUNDED_TIME |
---|---|
2023-03-08 08:01:00 | 2023-03-08 08:00:00 |
... | ... |
2023-03-08 08:07:00 | 2023-03-08 08:00:00 |
2023-03-08 08:08:00 | 2023-03-08 08:15:00 |
... | ... |
2023-03-08 08:22:00 | 2023-03-08 08:15:00 |
2023-03-08 08:23:00 | 2023-03-08 08:30:00 |
... | ... |
2023-03-08 08:37:00 | 2023-03-08 08:30:00 |
2023-03-08 08:38:00 | 2023-03-08 08:45:00 |
... | ... |
2023-03-08 08:52:00 | 2023-03-08 08:45:00 |
2023-03-08 08:53:00 | 2023-03-08 09:00:00 |
... | ... |
2023-03-08 09:00:00 | 2023-03-08 09:00:00 |
英文:
You can TRUNC
ate the time to the start of the hour and then round the minutes and seconds part to the nearest 15 minutes and add:
SELECT time,
TRUNC(time, 'HH') + ROUND((time - TRUNC(time, 'HH'))*4*24)/4/24 AS rounded_time
FROM times
Which, for the sample data:
CREATE TABLE times (time) AS
SELECT TRUNC(SYSDATE, 'HH') + NUMTODSINTERVAL(LEVEL, 'MINUTE')
FROM DUAL
CONNECT BY LEVEL <= 60;
Outputs:
TIME | ROUNDED_TIME |
---|---|
2023-03-08 08:01:00 | 2023-03-08 08:00:00 |
... | ... |
2023-03-08 08:07:00 | 2023-03-08 08:00:00 |
2023-03-08 08:08:00 | 2023-03-08 08:15:00 |
... | ... |
2023-03-08 08:22:00 | 2023-03-08 08:15:00 |
2023-03-08 08:23:00 | 2023-03-08 08:30:00 |
... | ... |
2023-03-08 08:37:00 | 2023-03-08 08:30:00 |
2023-03-08 08:38:00 | 2023-03-08 08:45:00 |
... | ... |
2023-03-08 08:52:00 | 2023-03-08 08:45:00 |
2023-03-08 08:53:00 | 2023-03-08 09:00:00 |
... | ... |
2023-03-08 09:00:00 | 2023-03-08 09:00:00 |
答案3
得分: 0
这是我的理解(带有一些示例数据和逐步计算;只使用 result
)。
SQL> with test (id, date_from, date_to) as
2 (select 1, to_date('08.03.2023 17:00', 'dd.mm.yyyy hh24:mi'), to_date('08.03.2023 20:25', 'dd.mm.yyyy hh24:mi') from dual union all
3 select 2, to_date('08.03.2023 17:00', 'dd.mm.yyyy hh24:mi'), to_date('08.03.2023 20:42', 'dd.mm.yyyy hh24:mi') from dual union all
4 select 3, to_date('08.03.2023 17:00', 'dd.mm.yyyy hh24:mi'), to_date('08.03.2023 20:06', 'dd.mm.yyyy hh24:mi') from dual union all
5 select 4, to_date('08.03.2023 17:00', 'dd.mm.yyyy hh24:mi'), to_date('08.03.2023 20:57', 'dd.mm.yyyy hh24:mi') from dual
6 ),
7 temp as
8 (select id, date_from, date_to,
9 numtodsinterval(date_to - date_from, 'day') diff
10 from test
11 )
12 select id,
13 to_char(date_from, 'hh24:mi') date_from,
14 to_char(date_to , 'hh24:mi') date_to,
15 --
16 diff,
17 extract(hour from diff) hh,
18 extract(minute from diff) mi,
19 --
20 case when extract(minute from diff) between 15 and 22 then 0.25
21 when extract(minute from diff) between 23 and 37 then 0.50
22 when extract(minute from diff) between 38 and 52 then 0.75
23 when extract(minute from diff) < 14 then 0.00
24 when extract(minute from diff) > 52 then 1.00
25 end rnd,
26 --
27 extract(hour from diff) +
28 case when extract(minute from diff) between 15 and 22 then 0.25
29 when extract(minute from diff) between 23 and 37 then 0.50
30 when extract(minute from diff) between 38 and 52 then 0.75
31 when extract(minute from diff) < 14 then 0.00
32 when extract(minute from diff) > 52 then 1.00
33 end result
34 from temp
35 order by id;
英文:
This is how I understood it (with some sample data and step-by-step calculation; you'd use result
only).
SQL> with test (id, date_from, date_to) as
2 (select 1, to_date('08.03.2023 17:00', 'dd.mm.yyyy hh24:mi'), to_date('08.03.2023 20:25', 'dd.mm.yyyy hh24:mi') from dual union all
3 select 2, to_date('08.03.2023 17:00', 'dd.mm.yyyy hh24:mi'), to_date('08.03.2023 20:42', 'dd.mm.yyyy hh24:mi') from dual union all
4 select 3, to_date('08.03.2023 17:00', 'dd.mm.yyyy hh24:mi'), to_date('08.03.2023 20:06', 'dd.mm.yyyy hh24:mi') from dual union all
5 select 4, to_date('08.03.2023 17:00', 'dd.mm.yyyy hh24:mi'), to_date('08.03.2023 20:57', 'dd.mm.yyyy hh24:mi') from dual
6 ),
Query:
7 temp as
8 (select id, date_from, date_to,
9 numtodsinterval(date_to - date_from, 'day') diff
10 from test
11 )
12 select id,
13 to_char(date_from, 'hh24:mi') date_from,
14 to_char(date_to , 'hh24:mi') date_to,
15 --
16 diff,
17 extract(hour from diff) hh,
18 extract(minute from diff) mi,
19 --
20 case when extract(minute from diff) between 15 and 22 then 0.25
21 when extract(minute from diff) between 23 and 37 then 0.50
22 when extract(minute from diff) between 38 and 52 then 0.75
23 when extract(minute from diff) < 14 then 0.00
24 when extract(minute from diff) > 52 then 1.00
25 end rnd,
26 --
27 extract(hour from diff) +
28 case when extract(minute from diff) between 15 and 22 then 0.25
29 when extract(minute from diff) between 23 and 37 then 0.50
30 when extract(minute from diff) between 38 and 52 then 0.75
31 when extract(minute from diff) < 14 then 0.00
32 when extract(minute from diff) > 52 then 1.00
33 end result
34 from temp
35 order by id;
ID DATE_FROM DATE_TO DIFF HH MI RND RESULT
--- ---------- ---------- ------------------------------ --- --- ----- ----------
1 17:00 20:25 +000000000 03:25:00.000000000 3 25 0,50 3,5
2 17:00 20:42 +000000000 03:42:00.000000000 3 42 0,75 3,75
3 17:00 20:06 +000000000 03:06:00.000000000 3 6 0,00 3
4 17:00 20:57 +000000000 03:57:00.000000000 3 57 1,00 4
SQL>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论