Re: [SQL ] 挑选A表中某条件值与B表做join

楼主: rockchangnew (rock)   2016-02-04 11:07:40
※ 引述《Schematic (小小宝的妈)》之铭言:
: 内容/问题描述:
: A表
: year id term status
: 1 90 1 1 1
: 2 90 1 2 1
: 3 91 1 1 1
: 4 91 1 2 1
: 5 91 1 3 4
: 6 92 1 1 5
: 7 90 2 1 1
: 8 90 2 2 1
: 9 91 2 1 3
: 10 90 3 1 1
: 11 90 3 2 6
: B表
: year id type
: 1 90 1 A
: 2 90 2 B
: 3 90 3 A
: 希望的结果是要将A表中同一id下year最大且term最大的那一列join到B表
: year id type status
: 1 90 1 A 5 (取A表的第6行的status值)
: 2 90 2 B 3 (取A表的第9行)
: 3 90 3 A 6 (取A表的第11行)
: 不知道语法要怎么下才会比较简洁
: 谢谢
https://goo.gl/photos/nmatoCvvQgK9tVwF8
with tmp_A as(
select row_number() over(partition by id order by [year] desc,[term] desc) as Num ,* From A)
Select b.*,a.status from tmp_A a
inner join B b on a.id=b.id
where a.num=1
作者: Schematic (小小宝的妈)   2016-02-04 11:21:00
非常谢谢您
作者: atm (风继续吹)   2016-02-12 09:56:00
请问这是什么软件的画面呢

Links booklink

Contact Us: admin [ a t ] ucptt.com