SQL: 最旧项目的年龄
SQL: age of oldest items
我有以下 Oracle 数据库。某个 ID 可以具有已接受状态,但可以在失败状态下继续,然后 return 回到已接受状态。这可能发生 X 次,所以它可以被称为循环
ID STATUS TIME INSERT
------------------------------
1 Accepted 01:00:00
1 Failed 02:00:00
1 Accepted 02:30:00
2 Accepted 02:33:00
我想计算具有已接受状态的最旧项目的年龄。
这意味着 SYSDATE - TIME INSERT
的结果
As Result of this data I expect the 3d record time value, 02:30:00
我需要检查每个 ID 的最年轻时间,以及 ID 的所有最年轻生命周期中最老的生命周期。
这对我来说看起来很复杂。
工作流程步骤为:
- 状态为Accepted的特定id的最小时间值
- 第 1 步针对每个处于已接受状态的 ID
- 我在步骤 2 中计算的所有最小值的最大值。
此时我有以下代码
select min(TO_CHAR(TO_DATE('20000101','yyyymmdd')+(SYSDATE - dm1.time_insert),'hh24:mi:ss'))
from db dm1
where dm1.status='accepted'
group by dm1.id;
这为我提供了所有记录的最小值序列。但是现在我需要所有这些最小值中的最大值,我该怎么做?
谁能破案?
希望对您有所帮助:
-- this will give you the minimum date of all id's
select * from tab1 where TIME_INSERT in (select min(TIME_INSERT) from tab1 )and STATUS ='Accepted'
-- this will give you the minimum date of a specefique id
select * from tab1 where TIME_INSERT in (select min(TIME_INSERT) from tab1 )
and STATUS ='Accepted' and ID =1
检查这个
select id, TIME_INSERT from
(select TIME_INSERT, ID , min(TIME_INSERT) over (partition by ID) maxid from tab1)
where TIME_INSERT = maxid and TIME_INSERT in(select max(TIME_INSERT) from tab1) group by ID,TIME_INSERT
您可以只查找 max(time_insert) 并按 id 分组。查看示例查询
with src as (
select 1 as id, 'Accepted' as status, to_date('2015-05-05 01:00:00','yyyy-mm-dd hh24:mi:ss') as time_insert from dual
union all
select 1 as id, 'Failed' as status, to_date('2015-05-05 02:00:00','yyyy-mm-dd hh24:mi:ss') as time_insert from dual
union all
select 1 as id, 'Accepted' as status, to_date('2015-05-05 02:30:00','yyyy-mm-dd hh24:mi:ss') as time_insert from dual
union all
select 2 as id, 'Accepted' as status, to_date('2015-05-05 02:33:00','yyyy-mm-dd hh24:mi:ss') as time_insert from dual)
select id, max(time_insert) as time_insert, max(sysdate - time_insert) as lifetime_max, min(sysdate - time_insert) as liftime_min from src
where status ='Accepted'
group by id
Edit: Is this what you want to achieve:
with src as (
select 1 as id, 'Accepted' as status, to_date('2015-05-05 01:00:00','yyyy-mm-dd hh24:mi:ss') as time_insert from dual
union all
select 1 as id, 'Failed' as status, to_date('2015-05-05 02:00:00','yyyy-mm-dd hh24:mi:ss') as time_insert from dual
union all
select 1 as id, 'Accepted' as status, to_date('2015-05-05 02:30:00','yyyy-mm-dd hh24:mi:ss') as time_insert from dual
union all
select 2 as id, 'Accepted' as status, to_date('2015-05-05 02:33:00','yyyy-mm-dd hh24:mi:ss') as time_insert from dual)
, src2 as (
select max(time_insert) as time_insert, max(sysdate - time_insert) as lifetime_max, min(sysdate - time_insert) as lifetime_min from src
where status ='Accepted')
select max(lifetime_min) from src2
这样就可以了。
SELECT id,
min(time_insert),
max(time_insert)
FROM
your_table t1
WHERE
status = 'Accepted'
AND time_insert > (
SELECT
nvl(max(time_insert), to_date('01-JAN-1970', 'DD-MON-YYYY'))
FROM
your_table
WHERE
status = 'Failed'
AND id = t1.id
)
GROUP BY id
我有以下 Oracle 数据库。某个 ID 可以具有已接受状态,但可以在失败状态下继续,然后 return 回到已接受状态。这可能发生 X 次,所以它可以被称为循环
ID STATUS TIME INSERT
------------------------------
1 Accepted 01:00:00
1 Failed 02:00:00
1 Accepted 02:30:00
2 Accepted 02:33:00
我想计算具有已接受状态的最旧项目的年龄。 这意味着 SYSDATE - TIME INSERT
的结果As Result of this data I expect the 3d record time value, 02:30:00
我需要检查每个 ID 的最年轻时间,以及 ID 的所有最年轻生命周期中最老的生命周期。 这对我来说看起来很复杂。
工作流程步骤为:
- 状态为Accepted的特定id的最小时间值
- 第 1 步针对每个处于已接受状态的 ID
- 我在步骤 2 中计算的所有最小值的最大值。
此时我有以下代码
select min(TO_CHAR(TO_DATE('20000101','yyyymmdd')+(SYSDATE - dm1.time_insert),'hh24:mi:ss'))
from db dm1
where dm1.status='accepted'
group by dm1.id;
这为我提供了所有记录的最小值序列。但是现在我需要所有这些最小值中的最大值,我该怎么做? 谁能破案?
希望对您有所帮助:
-- this will give you the minimum date of all id's
select * from tab1 where TIME_INSERT in (select min(TIME_INSERT) from tab1 )and STATUS ='Accepted'
-- this will give you the minimum date of a specefique id
select * from tab1 where TIME_INSERT in (select min(TIME_INSERT) from tab1 )
and STATUS ='Accepted' and ID =1
检查这个
select id, TIME_INSERT from
(select TIME_INSERT, ID , min(TIME_INSERT) over (partition by ID) maxid from tab1)
where TIME_INSERT = maxid and TIME_INSERT in(select max(TIME_INSERT) from tab1) group by ID,TIME_INSERT
您可以只查找 max(time_insert) 并按 id 分组。查看示例查询
with src as (
select 1 as id, 'Accepted' as status, to_date('2015-05-05 01:00:00','yyyy-mm-dd hh24:mi:ss') as time_insert from dual
union all
select 1 as id, 'Failed' as status, to_date('2015-05-05 02:00:00','yyyy-mm-dd hh24:mi:ss') as time_insert from dual
union all
select 1 as id, 'Accepted' as status, to_date('2015-05-05 02:30:00','yyyy-mm-dd hh24:mi:ss') as time_insert from dual
union all
select 2 as id, 'Accepted' as status, to_date('2015-05-05 02:33:00','yyyy-mm-dd hh24:mi:ss') as time_insert from dual)
select id, max(time_insert) as time_insert, max(sysdate - time_insert) as lifetime_max, min(sysdate - time_insert) as liftime_min from src
where status ='Accepted'
group by id
Edit: Is this what you want to achieve:
with src as (
select 1 as id, 'Accepted' as status, to_date('2015-05-05 01:00:00','yyyy-mm-dd hh24:mi:ss') as time_insert from dual
union all
select 1 as id, 'Failed' as status, to_date('2015-05-05 02:00:00','yyyy-mm-dd hh24:mi:ss') as time_insert from dual
union all
select 1 as id, 'Accepted' as status, to_date('2015-05-05 02:30:00','yyyy-mm-dd hh24:mi:ss') as time_insert from dual
union all
select 2 as id, 'Accepted' as status, to_date('2015-05-05 02:33:00','yyyy-mm-dd hh24:mi:ss') as time_insert from dual)
, src2 as (
select max(time_insert) as time_insert, max(sysdate - time_insert) as lifetime_max, min(sysdate - time_insert) as lifetime_min from src
where status ='Accepted')
select max(lifetime_min) from src2
这样就可以了。
SELECT id,
min(time_insert),
max(time_insert)
FROM
your_table t1
WHERE
status = 'Accepted'
AND time_insert > (
SELECT
nvl(max(time_insert), to_date('01-JAN-1970', 'DD-MON-YYYY'))
FROM
your_table
WHERE
status = 'Failed'
AND id = t1.id
)
GROUP BY id