Table_A ------------------- A_id ------------------- 1 Table_B ------------------- B_id | A_id ------------------- 1 1 2 1 3 1 Table_C ----------------------------- B_id | Process_date ----------------------------- 1 20130101 12:20:01 2 20130101 12:10:01 3 20130101 13:00:01
如何根据process_date
时间窗口引用Table_C
,从Table_A.A_id
检索最大Table_C
。如果我想在时间窗口Table_C.b_id
中检索max(process_date)
和20130101 12:09:00
到12:21:00
,则应将id返回为1,将process_date返回为12:20:01
以下查询我正在使用:
select b_id,
process_date
from (select c1.b_id,
c1.process_date,
row_number() over(partition by a.a_id
order by c1.process_date desc) rn
from table_a a
inner join
table_b b
on a.a_id = b.a_id
inner join
table_c c1
on b.b_id = c1.b_id
)
where rn = 1;
答案 0 :(得分:0)
如果我关注,您需要最大处理日期和给定a_id的相应b_id,其中处理日期在设定的日期范围内。这是一个不需要任何分析功能或行号的解决方案。我正在使用with子句和dual来复制样本表。如果你有table_a,table_b,table_c。
,你可以删除它with table_a as
(
select 1 a_id from dual
),
table_b as
(
select 1 b_id, 1 a_id from dual union all
select 2 b_id, 1 a_id from dual union all
select 3 b_id, 1 a_id from dual
), table_c as
(
select 1 b_id, to_date('20130101 12:20:01', 'yyyymmdd hh24:mi:ss') process_date from dual union all
select 2 b_id, to_date('20130101 12:10:01', 'yyyymmdd hh24:mi:ss') process_date from dual union all
select 3 b_id, to_date('20130101 13:00:01', 'yyyymmdd hh24:mi:ss') process_date from dual
)
select table_c.b_id,
table_c.process_date
from table_b,
table_c
where table_b.b_id = table_c.b_id
and table_b.a_id = 1
and table_c.process_date = (select max(process_date)
from table_b b2,
table_c c2
where table_b.a_id = b2.a_id
and b2.b_id = c2.b_id
and c2.process_date between to_date('20130101 12:09:00', 'yyyymmdd hh24:mi:ss') and
to_date('20130101 12:21:00', 'yyyymmdd hh24:mi:ss')
)
返回:
----------------------------
b_id | process_date
----------------------------
1 1/1/2013 12:20:01