日期:2014-05-17  浏览次数:20877 次

求教育,这个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