表A(id为主键,dw为单位)
id dw
1 01007
2 01007
3 01007
4 01008表B(id和i9999为联合主键,一个id根据i9999的值的不同对应有多个职称。)
id i9999 职称
1 1 副教授
1 2 教授
2 1 讲师
2 2 副教授
2 3 教授
3 1 讲师
3 2 副教授
想得到指定单位的每个id的i9999最大值对应的职称:
id i9999 职称
1 2 教授
2 3 教授
3 2 副教授纠结了一整天了,谢谢!
id dw
1 01007
2 01007
3 01007
4 01008表B(id和i9999为联合主键,一个id根据i9999的值的不同对应有多个职称。)
id i9999 职称
1 1 副教授
1 2 教授
2 1 讲师
2 2 副教授
2 3 教授
3 1 讲师
3 2 副教授
想得到指定单位的每个id的i9999最大值对应的职称:
id i9999 职称
1 2 教授
2 3 教授
3 2 副教授纠结了一整天了,谢谢!
from b b1
where not exists (
select 1
from b
where id = b1.id
and i9999>b1.i9999
)
from b b1
where i9999 = (
select max(i9999)
from b
where id = b1.id
)
from 表A a,表B b where a.id=b.i9999
and id=(select max(i9999) from 表B where id=b.id)
from b b1 ,a
where b1.id = a.id
and a.dw= '01007'
and not exists (
select 1
from b
where id = b1.id
and i9999>b1.i9999
)