从列表oracle sql中获取最接近的最高值

时间:2014-04-06 18:02:41

标签: sql oracle logic

我在数据库中有一个列,格式如下:yymmddhh24miss

Sample Data:
140203101241
140202101141
140102101240
143001101244
142801101245
142701131347
142601121542
142101131744
...

我需要从列表中获取最接近的高值。例如:如果我通过142701131333,那么它应该从上面的列表中返回142701131347。

任何帮助表示赞赏!

3 个答案:

答案 0 :(得分:1)

SELECT data
FROM
(
   SELECT data
   FROM tbl
   WHERE data > '142701131333'
   ORDER BY data
) a
WHERE rownum = 1

答案 1 :(得分:1)

SQL> with t (x) as (
  2  select 140203101241 from dual union all
  3  select 140202101141 from dual union all
  4  select 140102101240 from dual union all
  5  select 143001101244 from dual union all
  6  select 142801101245 from dual union all
  7  select 142701131347 from dual union all
  8  select 142601121542 from dual union all
  9  select 142101131744 from dual
 10  )
 11  select min(x) minx from t where x > 142701131333
 12  /

             MINX                                                               
-----------------                                                               
     142701131347          

答案 2 :(得分:1)

SELECT MIN(sample_data)
FROM tableName
WHERE sample_data > 142701131333