查询优化

shiyiwan 2009-07-21 05:25:37
问题描述:

有表test,有4个字段a,b,c,d,其中a,b,c是varchar2类型,d是date类型。
表中数据如下:
a b c d
1 1 2 01142009
1 1 3 01212009
2 1 4 01212009
2 1 5 01222009

需要得到的结果如下:

a b c d
1 1 3 01212009
2 1 5 01222009

即根据a,b分组,取出d最大的那条记录。不用row_number() over()等函数(因某些原因不支持),尽量不用子查询,
请给出一个查询效率高的方案。

目前使用的是先group by a,b,c,再查出按a,b分组中的最大d,效率很差
...全文
118 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
shiyiwan 2009-07-21
  • 打赏
  • 举报
回复
之前我怎么没想到呢

明天去试试
[Quote=引用 1 楼 bw555 的回复:]
SQL codeselect*from test tabawherenotexists (select1from testwhere a=taba.aand b=taba.band d>taba.d)
[/Quote]
shiyiwan 2009-07-21
  • 打赏
  • 举报
回复
现在我用的就是这种方法,效率不太好。

[Quote=引用 2 楼 robin_ares 的回复:]
select a.*
from
test a,
(select a,b,max(c) c from test group by a,b) a1
where a.a = a1.a
  and a.b = a1.b
  and a.c = a1.c

[/Quote]
suncrafted 2009-07-21
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 bw555 的回复:]
SQL codeselect*from test tabawherenotexists (select1from testwhere a=taba.aand b=taba.band d>taba.d)
[/Quote]

支持
mantisXF 2009-07-21
  • 打赏
  • 举报
回复
RIGHT![Quote=引用 1 楼 bw555 的回复:]
SQL codeselect*from test tabawherenotexists (select1from testwhere a=taba.aand b=taba.band d>taba.d)
[/Quote]
ks_reny 2009-07-21
  • 打赏
  • 举报
回复

select * from test
where not exists(select 1 from test a where a=a.a and b=a.b and d<a.d)
quiettown 2009-07-21
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 robin_ares 的回复:]
select a.*
from
test a,
(select a,b,max(c) c from test group by a,b) a1
where a.a = a1.a
  and a.b = a1.b
  and a.c = a1.c

[/Quote]
UP,除了把C换成D。好象也没有别的法子勒。。。
robin_ares 2009-07-21
  • 打赏
  • 举报
回复
select a.*
from
test a,
(select a,b,max(c) c from test group by a,b) a1
where a.a = a1.a
and a.b = a1.b
and a.c = a1.c
bw555 2009-07-21
  • 打赏
  • 举报
回复
select * from test taba
where not exists (select 1 from test where a=taba.a and b=taba.b and d>taba.d)

3,494

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 高级技术相关讨论专区
社区管理员
  • 高级技术社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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