//实例1: //ta表 A B C 1 2 3 1 2 4 1 2 5 1 2 6 //tb表 A B D 1 2 100 1 2 200 1 2 300 //结果: A B C D 1 2 3 100 1 2 4 200 1 2 5 300 1 2 6 //分析: //典型的左连接问题,但是这里需要将相应的行对应到指定的行上去, //我们使用row_number()函数来做: with ta as( select 1 a,2 b,3 c from dual union all select 1,2,4 from dual union all select 1,2,5 from dual union all select 1,2,6 from dual) ,tb as( select 1 a,2 b,100 c from dual union all select 1,2,200 from dual union all select 1,2,300 from dual) select a.a,a.b,a.c,b.c from (select ta.* ,row_number() over (partition by a,b order by rownum) rn from ta) a, left join (select tb.* ,row_number() over (partition by a,b order by rownum) rn from tb) b on a.a=b.a and a.b=b.b and a.rn=b.rn //where a.a=b.b(+) //我们知道,关于左连接有两种方式: //1.使用关键词 left join //2.使用 "+" //我们这里为什么使用关键词 left join 呢? //因为这里有多个连接条件,更易于使用 left join // //实例2 //下面是一张表, //取出每个NAME 对应时间对大的数据 NAME TYPE TIME 1 C2 12:00 1 C3 13:00 1 C5 14:00 2 C1 12:00 3 C4 12:00 4 C5 12:00 4 C6 14:00 //结果: NAME TYPE TIME 1 C5 14:00 2 C1 12:00 3 C4 12:00 4 C6 14:00 // with t as( select 1 name,'C2' type,'12:00' time from dual union all select 1,'C3','13:00' from dual union all select 1,'C5','14:00' from dual union all select 2,'C1','12:00' from dual union all select 3,'C4','12:00' from dual union all select 4,'C5','12:00' from dual union all select 4,'C6','14:00' from dual) select name,type,time from ( select name ,type ,time ,row_number() over (partition by name order by time desc) rn from t) a where a.rn=1 / NAME TYPE TIME ---------- ---- ----- 1 C5 14:00 2 C1 12:00 3 C4 12:00 4 C6 14:00 //row_number()评级函数是根据每一个分组,按照指定的列排序,然后返回一个rownum值 //上面两个实例,我们都借助了row_number()函数,返回一个分组排序后的行编号, //然后根据具体的需要,使用这个行编号 原帖:http://topic.csdn.net/u/20090618/09/1795e0bc-b874-4fb1-8959-799231482bce.html?67906 row_number()函数: http://blog.csdn.net/BOBO12082119/archive/2011/04/01/6294889.aspx