table1 (A1,B1)
table2 (A2,B2,C2)
table1中数据:13632511111 10666666 N1
13632511111 10666666 N2
13521451111 10654412 N1
13521451111 10654412 N2table2中数据13632511111 10666666 20100921 14:10:00
13632511111 10666666 20100921 11:11:01
13632511111 10666666 20100921 14:12:00
13632511111 10666666 20100921 14:13:0013521451111 10654412 20100921 14:10:00
13521451111 10654412 20100921 14:11:00
13521451111 10654412 20100921 14:12:00
13521451111 10654412 20100921 14:13:00也就是说table1与table2 中的数据可根据A1,B1来对应.但现在只需要将表2中时间较小的几条对应上去。
结果应该是这样:13632511111 10666666 N1 20100921 14:10:00
13632511111 10666666 N2 20100921 11:11:01
13521451111 10654412 N1 20100921 14:10:00
13521451111 10654412 N2 20100921 14:11:00请问这语句怎么写。
分不够可另开贴加分
table2 (A2,B2,C2)
table1中数据:13632511111 10666666 N1
13632511111 10666666 N2
13521451111 10654412 N1
13521451111 10654412 N2table2中数据13632511111 10666666 20100921 14:10:00
13632511111 10666666 20100921 11:11:01
13632511111 10666666 20100921 14:12:00
13632511111 10666666 20100921 14:13:0013521451111 10654412 20100921 14:10:00
13521451111 10654412 20100921 14:11:00
13521451111 10654412 20100921 14:12:00
13521451111 10654412 20100921 14:13:00也就是说table1与table2 中的数据可根据A1,B1来对应.但现在只需要将表2中时间较小的几条对应上去。
结果应该是这样:13632511111 10666666 N1 20100921 14:10:00
13632511111 10666666 N2 20100921 11:11:01
13521451111 10654412 N1 20100921 14:10:00
13521451111 10654412 N2 20100921 14:11:00请问这语句怎么写。
分不够可另开贴加分
select a2,b2,c2,row_number()over(partition by a2,b2 order by c2) rn
from table2 b
where exists(select 1 from table1 a where a.a1=b.a2 and a.b1=b.b2)
)
where rn<=2
where t1.b1=t2.b2
order by t2.c2) where rn<10
具体列名哥不想写了,你自己把*改成列名就可以了
select A2,B2
from
(select A2,B2,C2,row_number() over(partition by A2,B2 order by C2 desc) rn
from table2) a,table1 b
where a.A2=b.A1 and a.B2=b.B1 and a.rn<3
select A2,B2
from
(select A2,B2,C2,row_number() over(partition by A2,B2 order by C2) rn
from table2) a,table1 b
where a.A2=b.A1 and a.B2=b.B1 and a.rn<3
--楼主,你要的答案!
select A2,B2
from
(select A2,B2,C2,row_number() over(partition by A2,B2 order by C2) rn
from table2) a,table1 b
where a.A2=b.A1 and a.B2=b.B1 and a.rn<=2;
Connected as scott
SQL> select * from table1;
A1 B1
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
13632511111 10666666
13632511111 10666666
13521451111 10654412
13521451111 10654412
SQL> select * from table2;
A2 B2 C2
-------------------------------------------------------------------------------- -------------------------------------------------------------------------------- --------------------------------------------------------------------------------
13632511111 10666666 20100921 14:10:00
13632511111 10666666 20100921 11:11:01
13632511111 10666666 20100921 14:12:00
13632511111 10666666 20100921 14:13:00
13521451111 10654412 20100921 14:10:00
13521451111 10654412 20100921 14:11:00
13521451111 10654412 20100921 14:12:00
13521451111 10654412 20100921 14:13:00
8 rows selected
SQL>
SQL> select a.a1,a.b1,b.c2 from
2 (select a1,b1,row_number()over(partition by a1,b1 order by a1,b1) rna from table1) a,
3 (select a2,b2,c2,row_number()over(partition by a2,b2 order by c2 ) rnb from table2 ) b
4 where a.a1=b.a2(+) and a.b1=b.b2(+) and a.rna=b.rnb(+);
A1 B1 C2
-------------------------------------------------------------------------------- -------------------------------------------------------------------------------- --------------------------------------------------------------------------------
13521451111 10654412 20100921 14:10:00
13521451111 10654412 20100921 14:11:00
13632511111 10666666 20100921 11:11:01
13632511111 10666666 20100921 14:10:00
SQL>