//实例1://ta表A B C1 2 31 2 41 2 51 2 6//tb表A B D1 2 1001 2 2001 2 300//结果:A B C D1 2 3 1001 2 4 2001 2 5 3001 2 6//分析://典型的左连接问题,但是这里需要将相应的行对应到指定的行上去,//我们使用row_number()函数来做:with ta as(select 1 a,2 b,3 c from dual union allselect 1,2,4 from dual union allselect 1,2,5 from dual union allselect 1,2,6 from dual),tb as(select 1 a,2 b,100 c from dual union allselect 1,2,200 from dual union allselect 1,2,300 from dual)select a.a,a.b,a.c,b.cfrom (select ta.*,row_number() over (partition by a,b order by rownum) rnfrom ta) a,left join(select tb.*,row_number() over (partition by a,b order by rownum) rnfrom tb) bon a.a=b.aand a.b=b.band 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:001 C3 13:001 C5 14:002 C1 12:003 C4 12:004 C5 12:004 C6 14:00//结果:NAME TYPE TIME 1 C5 14:002 C1 12:003 C4 12:004 C6 14:00//with t as(select 1 name,'C2' type,'12:00' time from dual union allselect 1,'C3','13:00' from dual union allselect 1,'C5','14:00' from dual union allselect 2,'C1','12:00' from dual union allselect 3,'C4','12:00' from dual union allselect 4,'C5','12:00' from dual union allselect 4,'C6','14:00' from dual)select name,type,timefrom (select name,type,time,row_number() over (partition by name order by time desc) rnfrom t) awhere a.rn=1/NAME TYPE TIME---------- ---- -----1 C5 14:002 C1 12:003 C4 12:004 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