求助 只用SQL 能实现这个查询吗

hebian001 2013-07-03 05:47:40
id time code
1 0703 a
1 0702 a
1 0701 b
1 0630 a
2 0703 a
2 0702 a
2 0701 a
2 0630 b
2 0629 b
2 0628 c

.....
有上述表格,要求查询结果是

id time code
1 0703 a
1 0701 b
1 0630 a
2 0703 a
2 0630 b
2 0628 c
就是order by id, time. 如果是连续code相同的取time最大的那一行,
其他的都不要。能在一个sql里查询出来吗?
...全文
343 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
小李木耳 2013-07-04
  • 打赏
  • 举报
回复
表达有误: 2 0703 a 2 0702 a 2 0701 a --》 1 0630 a 2 0703 a 以上应该是:就是order by id, time. 如果是连续“ID”、【<--少了这个】 code相同的取time最大的那一行,
哥眼神纯洁不 2013-07-03
  • 打赏
  • 举报
回复

with tb as(
select 1 as id,'0703' as time,'a' as code from dual union all
select 1,'0702','a' from dual union all
select 1,'0701','b' from dual union all
select 1,'0630','a' from dual union all
select 2,'0703','a' from dual union all
select 2,'0702','a' from dual union all
select 2,'0701','a' from dual union all
select 2,'0630','b' from dual union all
select 2,'0629','b' from dual union all
select 2,'0628','c' from dual)
select id,time,code from(
select id,time,code,(select min(time) from tb where a.id=id and a.time<time )a from tb a 
)a
 where nvl((select code from tb where id=a.id and a=time),'$')!=code
sych888 2013-07-03
  • 打赏
  • 举报
回复
select t.id,t.time.t.code from (selet id,time,code,row_number() over(partition by id,code order by time desc) rn from tab_name) t where t.rn=1;

17,140

社区成员

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

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