0

我有一个名为 service_t 的表,它有一个列 Effective_dt,其中填充了 unix 时间戳。我需要找到最大有效dt 的所有行,但有效dt 必须小于给定值。我有以下sql,但我认为它效率不高:

Select *
  from service_t t1
 where t1.effective_dt <= :given_value
   and t1.effective_dt = (select max(effective_dt)
                            from service_t t2
                           where t2.effective_dt <= :given_value
                             and t1.id = t2.id)

这是有效的还是任何其他好的方法?谢谢!

4

1 回答 1

2

使用分析函数可能更有效:

Select * from (
  select t1.*, 
       dense_rank() over (partition by id order by effective_dt desc) rn
  from service_t t1
 where t1.effective_dt <= :given_value)
where rn = 1
于 2012-10-05T14:21:59.033 回答