如何根据模糊值取得边界值

childe 2003-04-29 12:57:22
比如有一个表tab1,数据为:
col1 col2 col3
10 abc def
20 hij dea
30 haha jiji

(col1不一定是整数可以是任意类型的数字,比如小数。)

给定一个值,16 可以得到10,和20两行
给定一个值,23 可以得到20,和30两行

如何用尽量少的语句查询??
...全文
75 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
Lastdrop 2003-04-30
  • 打赏
  • 举报
回复
我有点误解了你的问题,那你还是用 more_zou(小人物) 的SQL吧,不过用union all效率高一点,因为两个集合不可能有重复项的。

select max(col1) from tab where col1<val
union all
select min(col1) from tab where col1>val;
beckhambobo 2003-04-30
  • 打赏
  • 举报
回复
利用分折函数,试一试吧,还没直正测试过.
select * from table_name where col1 in (select col from (select col1,lead(col1,1,null) over(order by col1) next_col1 from table_name order by col1) where decode(sign(col1-值)+sign(next_col1-值),0,1)=0 and rownum<3)
childe 2003-04-29
  • 打赏
  • 举报
回复
谢谢 Lastdrop(空杯):

不过,col1是任意的数字型

col1 col2 col3
1.15 abc def
1.20 hij dea
1.25 haha jiji

输入1.18 或 1.21 就不对啦。

Lastdrop 2003-04-29
  • 打赏
  • 举报
回复
to: more_zou(小人物)
你的SQL最多只能取到两列,如果有第三列满足要求呢? -)
more_zou 2003-04-29
  • 打赏
  • 举报
回复
select max(col1) from tab where col1<val
union
select min(col1) from tab where col1>val;
Lastdrop 2003-04-29
  • 打赏
  • 举报
回复
select * from yourtable
where col1 between floor(val/10)*10 and ceil(val/10)*10

val 就是16或23
childe 2003-04-29
  • 打赏
  • 举报
回复
拜托拜托
beckhambobo 2003-04-29
  • 打赏
  • 举报
回复
建义用分折函数,比较可取而且简单。具体做法迟些给你.

17,377

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 基础和管理
社区管理员
  • 基础和管理社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧