SQL:结合大小写和数字到日期的转换
SQL: combine case and number to date conversion
我想做一个 sql 语句,它在一个 table 中计算 2 个日期值的减法。如果该值为负数,我只想将其显示为 0 值。
数值是付款处于当前状态的秒数。
我用一个技巧将它转换为时间值(日期类型)。
我当前的代码是
SELECT
max(CASE WHEN t1.time_event < SYSDATE and t2.time_event > SYSDATE THEN to_char(to_date(max(round(SYSDATE - t1.time_event) * 24 * 60 * 60)),'ssssss'),'hh24:mi:ss') else to_char(to_date(0)) END) as "current_age"
from tbl_dummyfeed t1 join tbl_dummyfeed t2 on t1.payment_Id = t2.payment_id
where t1.event = 'accepted' and t2.event = 'enriched');
这行得通,只需要转换为时间 hh24:mi:ss。
SELECT max(CASE WHEN t1.time_event < SYSDATE and t2.time_event > SYSDATE THEN round((SYSDATE - t1.time_event) * 24 * 60 * 60) else 0 END) as "current_age"
from tbl_dummyfeed t1 join tbl_dummyfeed t2 on t1.payment_id= t2.payment_id
where t1.event = 'accepted' and t2.event = 'enriched';
当我将转换添加到 hh24:mm:ss 时,解决方案如下所示
SELECT to_char(to_date(max(CASE WHEN t1.time_event < SYSDATE and t2.time_event > SYSDATE THEN round((SYSDATE - t1.time_event) * 24 * 60
* 60) else 0 END),'sssss'),'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 t2.event = 'enriched';
这是我的问题唯一好的解决方案。希望这对人们有所帮助。
您可以使用类似的日期 'trick',即将小数天差添加到时间部分为午夜的标称日期 - 您可以使用固定日期,或者 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;
您还可以使用分析方法,这样您只需点击 table 一次,子查询将每个 'enriched' 时间与前一个 'accepted' 时间配对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;
我想做一个 sql 语句,它在一个 table 中计算 2 个日期值的减法。如果该值为负数,我只想将其显示为 0 值。
数值是付款处于当前状态的秒数。 我用一个技巧将它转换为时间值(日期类型)。
我当前的代码是
SELECT
max(CASE WHEN t1.time_event < SYSDATE and t2.time_event > SYSDATE THEN to_char(to_date(max(round(SYSDATE - t1.time_event) * 24 * 60 * 60)),'ssssss'),'hh24:mi:ss') else to_char(to_date(0)) END) as "current_age"
from tbl_dummyfeed t1 join tbl_dummyfeed t2 on t1.payment_Id = t2.payment_id
where t1.event = 'accepted' and t2.event = 'enriched');
这行得通,只需要转换为时间 hh24:mi:ss。
SELECT max(CASE WHEN t1.time_event < SYSDATE and t2.time_event > SYSDATE THEN round((SYSDATE - t1.time_event) * 24 * 60 * 60) else 0 END) as "current_age"
from tbl_dummyfeed t1 join tbl_dummyfeed t2 on t1.payment_id= t2.payment_id
where t1.event = 'accepted' and t2.event = 'enriched';
当我将转换添加到 hh24:mm:ss 时,解决方案如下所示
SELECT to_char(to_date(max(CASE WHEN t1.time_event < SYSDATE and t2.time_event > SYSDATE THEN round((SYSDATE - t1.time_event) * 24 * 60
* 60) else 0 END),'sssss'),'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 t2.event = 'enriched';
这是我的问题唯一好的解决方案。希望这对人们有所帮助。
您可以使用类似的日期 'trick',即将小数天差添加到时间部分为午夜的标称日期 - 您可以使用固定日期,或者 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;
您还可以使用分析方法,这样您只需点击 table 一次,子查询将每个 'enriched' 时间与前一个 'accepted' 时间配对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;