一个简单的oracle分析函数问题,请高手指点一下

woodheart 2009-04-22 09:45:44
表tab,列class, name, seqnum, highpoint,lowpoint
数据
class seqnum highpoint lowpoint name
A 1 100 95 a
A 2 100 90 b
A 3 90 85 c
A 4 70 55 d
B 1 98 88 e
B 2 85 68 f
目的是取出各个class中,seqnum连续但是最高分比前面的seqnum的最低分要低的数据
例子里面的结果是
A 4 70 55 d
B 2 85 68 f

当然简单的方法是
select x.* from tab x, tab y where x.class = y.class and x.seqnum = y.seqnum+1 and x.highpoint<y.lowpoint
但是由于tab这个表的数据量非常大,不知能否用分析函数来实现?
...全文
135 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
wangrui4917 2009-04-28
  • 打赏
  • 举报
回复
学习了~~
superhsj 2009-04-22
  • 打赏
  • 举报
回复
这个只要涉及到排序的查询应该一层select很难处理吧,除非数据是按顺序插入到表中的。
用分析函数的好处是避免的自连接,另外我那个的结果能贴一下吗,我看看哪里的问题
woodheart 2009-04-22
  • 打赏
  • 举报
回复
这个结果不对的
另外,如果采用双重循环的话,我也能实现,只是双重循环的话,由于数据量大
也不好处理
不知有没有像having子句之类的方法来实现,一个select语句直接筛选出结果
[Quote=引用 5 楼 superhsj 的回复:]


SQL codeselect class,seqnum,highpoint,lowpoint,name
from(select class,seqnum,highpoint,lowpoint,name,lag(lowpoint,1,111)over (order by class,seqnum) low1
from table order by class,seqnum)
where highpoint<low1 order by class,name;
[/Quote]
superhsj 2009-04-22
  • 打赏
  • 举报
回复

select class,seqnum,highpoint,lowpoint,name
from(select class,seqnum,highpoint,lowpoint,name,lag(lowpoint,1,111)over (order by class,seqnum) low1
from table order by class,seqnum)
where highpoint<low1 order by class,name;
woodheart 2009-04-22
  • 打赏
  • 举报
回复
都展现出来
[Quote=引用 3 楼 superhsj 的回复:]
这个如果每个class满足条件的记录有多条怎么处理?
[/Quote]
superhsj 2009-04-22
  • 打赏
  • 举报
回复
这个如果每个class满足条件的记录有多条怎么处理?
woodheart 2009-04-22
  • 打赏
  • 举报
回复
没有仔细看我的问题
[Quote=引用 1 楼 hebo2005 的回复:]
你的示例数据不好,按你的示例数据直接取每条SEQ最大的就行了
但按你的要求来看,可能会有其它情况
[/Quote]
hebo2005 2009-04-22
  • 打赏
  • 举报
回复
你的示例数据不好,按你的示例数据直接取每条SEQ最大的就行了
但按你的要求来看,可能会有其它情况
superhsj 2009-04-22
  • 打赏
  • 举报
回复
哦,这个确实没想到
[Quote=引用 11 楼 woodheart 的回复:]
呵呵,还有个错误,没有partition,这样会导致name=e这一条的low1不对
改成这样就可以了,效率应该一般吧
select class,seqnum,highpoint,lowpoint,name
from(select class,seqnum,highpoint,lowpoint,name,lag(lowpoint,1,0)over (partition by
class order by seqnum) low1
from tmp_tab)
where highpoint <low1 order by class,name;
[/Quote]
woodheart 2009-04-22
  • 打赏
  • 举报
回复
呵呵,还有个错误,没有partition,这样会导致name=e这一条的low1不对
改成这样就可以了,效率应该一般吧
select class,seqnum,highpoint,lowpoint,name
from(select class,seqnum,highpoint,lowpoint,name,lag(lowpoint,1,0)over (partition by
class order by seqnum) low1
from tmp_tab)
where highpoint<low1 order by class,name;

[Quote=引用 9 楼 superhsj 的回复:]
哦,多了一条,lag初始值弄错了,呵呵。

SQL codeselect class,seqnum,highpoint,lowpoint,name
from(select class,seqnum,highpoint,lowpoint,name,lag(lowpoint,1,0)over (order by class,seqnum) low1
from table order by class,seqnum)
where highpoint<low1 order by class,name;



这个执行起来很慢吗,把里层的order by 去了试试还慢吗


SQL codeselect class,seqnum,highpoint,lowpoint,name
from(se…
[/Quote]
zhouxingyu896 2009-04-22
  • 打赏
  • 举报
回复
楼上的利害
学习
superhsj 2009-04-22
  • 打赏
  • 举报
回复
哦,多了一条,lag初始值弄错了,呵呵。
select class,seqnum,highpoint,lowpoint,name
from(select class,seqnum,highpoint,lowpoint,name,lag(lowpoint,1,0)over (order by class,seqnum) low1
from table order by class,seqnum)
where highpoint<low1 order by class,name;


这个执行起来很慢吗,把里层的order by 去了试试还慢吗

select class,seqnum,highpoint,lowpoint,name
from(select class,seqnum,highpoint,lowpoint,name,lag(lowpoint,1,0)over (order by class,seqnum) low1
from table)
where highpoint<low1 order by class,name;


[Quote=引用 8 楼 woodheart 的回复:]
你的结果
A 1 100 95 a
A 4 70 55 d
B 2 85 68 f
是的,我也在想一层循环很难,不知以后oracle会不会有这种分析的同时进行删除的功能
引用 7 楼 superhsj 的回复:
这个只要涉及到排序的查询应该一层select很难处理吧,除非数据是按顺序插入到表中的。
用分析函数的好处是避免的自连接,另外我那个的结果能贴一下吗,我看看哪里的问题
[/Quote]
woodheart 2009-04-22
  • 打赏
  • 举报
回复
你的结果
A 1 100 95 a
A 4 70 55 d
B 2 85 68 f
是的,我也在想一层循环很难,不知以后oracle会不会有这种分析的同时进行删除的功能
[Quote=引用 7 楼 superhsj 的回复:]
这个只要涉及到排序的查询应该一层select很难处理吧,除非数据是按顺序插入到表中的。
用分析函数的好处是避免的自连接,另外我那个的结果能贴一下吗,我看看哪里的问题
[/Quote]

17,088

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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