首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > oracle >

求教育,这个sql应该如何写

2013-08-04 
求教育,这个sql应该怎么写。有这么一个表id no time name1 1 2012-01-01 a1 2 2012-01-01 b2 1 2013-12-12

求教育,这个sql应该怎么写。
有这么一个表
id no time name
1 1 2012-01-01 a
1 2 2012-01-01 b
2 1 2013-12-12 a
3 1 2013-02-04 c
...
现在想做一个视图,如果有两行数据,其中ID,TIME两列值相同(类似于前两行),则只显示no比较小的那条,如果没有相同的,则正常显示
结果类似
id no time name
1 1 2012-01-01 a //过滤掉了第二行
2 1 2013-12-12 a
3 1 2013-02-04 c
...

[解决办法]
select a.* from a where a.no = (
select min(no) from b where b.id = a.id and b.time= a.time);

可以结贴了。
[解决办法]
楼上的会不会有问题,当no不是唯一码的时候会取多的,如果要这样用的话请用rowid。
或者partition by,例如:
select * from (
select id,no,time ,name ,row_number() over(partition by time,id order
by no asc) rowno from 
(select 1 id,1 no,'2012-01-01' time,'a' name from dual
union select 1 id,2 no,'2012-01-01' time,'a' name from dual
union select 2 id,1 no,'2013-12-12' time,'a' name from dual
union select 3 id,1 no,'2013-02-04' time,'a' name from dual
)t
)
where rowno=1

热点排行