0
表_A
------------------
援助          
------------------
1     

表_B
------------------
B_id | 援助
------------------
1 1
2 1
3 1

表_C
-----------------------------------------
B_id | 处理日期
-----------------------------------------
1 20130101 12:20:01
2 20130101 12:10:01
3 20130101 13:00:01

如何process_date从基于时序窗口Table_C的参考中检索最大值。如果我想检索并在计时窗口中,那么它应该返回 id 作为 1 和 process_date 作为Table_A.A_idTable_CTable_C.b_idmax(process_date)20130101 12:09:0012:21:0012: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;
4

1 回答 1

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
于 2013-03-06T23:51:30.283 回答