您可以使用類似的日期'技巧',即將小數天差額添加到名義日期(其中時間部分爲午夜) - 您可以使用固定日期或trunc(sysdate)
,只要時間最終成爲午夜 - 不必乘以24 * 60 * 60。 (您的to_date()
解決方案隱含地執行相同的操作,有效地將當前月份第一天的午夜秒數加上;但這可能會更清晰一些)。但你也可以移動case
條款爲where
過濾
select to_char(date '1970-01-01'
+ nvl(max(sysdate - t1.time_event), 0), 'HH24:MI:SS') as "current_age"
from tbl_dummyfeed t1
join tbl_dummyfeed t2
on t1.trax_id = t2.trax_id
where t1.event = 'accepted'
and t1.time_event < sysdate
and t2.event = 'enriched'
and t2.time_event > sysdate;
你也可以使用一個分析方法,所以你只需要打表一次,用一個子查詢對了每個「富」的時間與以往「接受」時間該ID,並且您然後過濾針對當前時間:
select to_char(date '1970-01-01'
+ nvl(max(sysdate - time_accepted), 0), 'HH24:MI:SS') as "current_age"
from (
select last_value(case when event = 'accepted' then time_event end ignore nulls)
over (partition by trax_id order by time_event) as time_accepted,
case when event = 'enriched' then time_event end as time_enriched
from tbl_dummyfeed
)
where time_accepted < sysdate
and time_enriched > sysdate;
添加'基於'sysdate' oracle'標籤和'TO_CHAR()'用法 – 2015-03-03 08:49:08
MAX是一個聚集功能,即只一行返回(只要沒有GROUP BY。)那麼爲什麼DISTINCT? – jarlh 2015-03-03 08:58:01
好的,我刪除了不同的,但我不認爲這是商業案例。 – Lumpi 2015-03-03 09:08:07