表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          副教授纠结了一整天了,谢谢!

解决方案 »

  1.   

    select *
    from b b1 
    where not exists (
    select 1
    from b
    where id = b1.id
    and i9999>b1.i9999
    )
      

  2.   

    or:select *
    from b b1 
    where i9999 = (
    select max(i9999)
    from b
    where id = b1.id
    )
      

  3.   

    2005+还有row_number写法,懒得写了
      

  4.   

    A.dw='01007',忘记写了,谢谢两位大佬,感激。
      

  5.   

    A.dw='01007',忘记写了。完整的语句应该怎么写呢?谢谢两位大佬,感激不尽。
      

  6.   

    select B.* from A left join B on A.id=B.id where A.dw='01007' and not exists (select * from B bb where B.id=bb.id and B.i9999<bb.i9999)
      

  7.   

    select *
    from 表A a,表B b where a.id=b.i9999
     and id=(select max(i9999) from 表B where id=b.id)
      

  8.   

    select b1.*
    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
    )
      

  9.   

    select B.* from A left join B on A.id=B.id where A.dw='01007' and not exists (select 1 from B bb where B.id=bb.id and B.i9999<bb.i9999)
      

  10.   

    谢谢了,在这试了一整天了,唉ASP.NET和SQL SERVER全是自学,写一些代码自己单位用,真不容易。任何一点的帮助都感激不尽,现在去结贴,散分,谢谢各位老大。