Table_A
A_id
1
Tale_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
如何从Table_C中检索基于Table_C时间窗口的Table_A A_id的最大process_date。如果我想在时间窗口20130101 12:09:00到12:21:00检索Table_C id和max(process_date),那么它应该将id返回为1,将process_date返回为12:20:01
答案 0 :(得分:1)
您可以使用获取max(process_date)
:
select c1.b_id,
c2.MaxDate
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
inner join
(
select max(process_date) MaxDate
from table_c
) c2
on c1.process_date = c2.maxdate;
或者您可以使用row_number()
:
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
答案 1 :(得分:0)
此查询应该为process_date
中的每个B_id
提供Table_A
的最大值SELECT c.B_id, MAX(Process_date)
FROM Table_C c
INNER JOIN Table_B b
ON b.B_id = c.B_id
INNER JOIN Table_A a
ON a.A_ID = b.A_id
GROUP BY c.B_id
:
c.B_id
如果您希望所有B_id的最大GROUP BY
引用process_date
,请从选择中删除Table_A
,{{1}}。