只有一个用户answer this correctly for TSQL,但是由于没有DATEDIFF函数,因此想知道如何现在最好在SQL Developer / PLSQL中实现此目的。
我要查询的表具有一些“ CODE”值,这些值自然可以在表“ Occs”中具有多个主键记录(“ OccsID”)。每个OccsID都有一个名为“ CreateDT”的日期时间列。
只是想根据“ CODE”在“ Occs”中的任何两个连续行之间找到最大可能的时间方差。
答案 0 :(得分:0)
如果您减去“下一个”日期和“这个”日期(使用LEAD
分析函数),则会得到日期差。然后获取每个code
的最大差异。像这样:
with diff as
(select occsid,
code,
nvl(lead(createdt) over (partition by code order by createdt), createdt) - createdt date_diff
from test
)
select code,
max(date_diff)
from diff
group by code;
答案 1 :(得分:0)
假设此T-SQL版本对您有效(来自上一个问题)
SELECT x.code, MAX(x.diff_sec) FROM
(
SELECT
code,
DATEDIFF(
SECOND,
CreateDT,
LEAD(CreateDT) OVER(PARTITION BY CODE ORDER BY CreateDT) --next row's createdt
) as diff_sec
FROM Occs
)x
GROUP BY x.code
最简单的选择是将两个日期相减,以天为单位。然后,您可以乘以小时,分钟或秒来获取差异
SELECT x.code, MAX(x.diff_day), MAX(x.diff_sec)
FROM
(
SELECT
code,
CreateDT -
LEAD(CreateDT) OVER(PARTITION BY CODE ORDER BY CreateDT) as diff_day,
24*60*60* (CreateDT -
LEAD(CreateDT) OVER(PARTITION BY CODE ORDER BY CreateDT)) as diff_sec
FROM Occs
)x
GROUP BY x.code