问题描述
我很好奇,看看如何在Oracle 12c中采用时间戳数据类型并将记录转换为EPOCH时间以使其成为数字,然后使用该数字查找该日期列中每个记录都在1分钟以内的任何记录其他(假设需要的话是同一天,或者只是1分钟内的任何计算)。
我尝试了以下操作,但得到了ORA-01873:间隔的前导精度误差太小。
从test1中选择(sold_date-to_date('1970-01-01 00:00:00','YYYY-MM-DD HH24:MI:SS'))* 86400作为epoch_sold_date;
解决方法
什么是SOLD_DATE
?例如SYSDATE
(返回DATE
数据类型的函数),您的代码即可正常工作。
SQL> select (sysdate
2 - to_date('1970-01-01 00:00:00','YYYY-MM-DD HH24:MI:SS')
3 ) * 86400 as epoch_sold_date
4 from dual;
EPOCH_SOLD_DATE
---------------
1600807918
SQL>
SOLD_DATE
是一个时间戳,但是-您似乎对一秒钟都不感兴趣,可以将其转换为DATE
:
select (cast (systimestamp as date) --> this
- to_date('1970-01-01 00:00:00','YYYY-MM-DD HH24:MI:SS')
) * 86400 as epoch_sold_date
from dual;
假设您在所有行上都得到相同的结果:嗯,我没有,如果SOLD_DATE
不同,您也不应该。
SQL> with test (sold_date) as
2 (select timestamp '2020-09-22 00:00:00.000000' from dual union all
3 select timestamp '2015-03-18 00:00:00.000000' from dual
4 )
5 select sold_date,6 (cast (sold_date as date)
7 - to_date('1970-01-01 00:00:00','YYYY-MM-DD HH24:MI:SS')
8 ) * 86400 as epoch_sold_date
9 from test;
SOLD_DATE EPOCH_SOLD_DATE
------------------------------ ---------------
22.09.20 00:00:00,000000000 1600732800
18.03.15 00:00:00,000000000 1426636800
SQL>
再进行一次编辑:当减去两个时间戳时,结果为interval day to second
。如果您从中提取分钟,您将获得所需的内容:
SQL> with test (sold_date) as
2 (select timestamp '2020-09-22 10:15:00.000000' from dual union all
3 select timestamp '2015-03-18 08:05:00.000000' from dual
4 )
5 select sold_date,6 lead(sold_date) over (order by sold_date) next_sold_date,7 --
8 lead(sold_date) over (order by sold_date) - sold_date diff,9 --
10 extract(day from lead(sold_date) over (order by sold_date) - sold_date) diff_mins
11 from test
12 order by sold_date;
SOLD_DATE NEXT_SOLD_DATE DIFF DIFF_MINS
------------------------------ ------------------------------ ------------------------------ ----------
18.03.15 08:05:00,000000000 22.09.20 10:15:00,000000000 +000002015 02:10:00.000000000 2015
22.09.20 10:15:00,000000000
SQL>
在您的情况下,您需要检查提取的分钟值是否大于1
(分钟)。
如果您只想查看两个时间戳之间有多少分钟,那么
- 将它们投射到日期上
- 减去这些日期(您将获得天数)
- 将其乘以24(一天中有24小时)和60(因为一小时中有60分钟)
类似这样的东西:
SQL> with test (date_1,date_2) as
2 (select timestamp '2020-09-22 10:15:00.000000',3 timestamp '2020-09-22 08:05:00.000000' from dual
4 )
5 select (cast(date_1 as date) - cast(date_2 as date)) * 24 * 60 diff_minutes
6 from test;
DIFF_MINUTES
------------
130
SQL>
,
如果您只是想比较日期并查找彼此之间在一分钟之内的行,则无需使用纪元时间。 How to print all the button texts within the url using Selenium through Java上有几种解决此问题的方法。