两个表,a表和b表
a表中有部门编号(bh)和部门(bm)两个字段
b表中有部门编号(bh)和人员(ry)两个字段
一个部门中可以由多个人,也可以有一个人
我现在是想把
a和b表连起来,只取每个部门中的一个人就行了,如何写sql语句
非常着急谢谢大家

解决方案 »

  1.   

    可以这样写,但效率有点低:
    select *,(select ry from b where a.bh=b.bh and rownum = 1) as ry 
    from a
      

  2.   

    楼主说的意思不大清楚
    关注ing
      

  3.   

    一个人是什么意思,随便一个人吗?
    SELECT A.BM,MIN(B.RY) AS RY
    FROM A,B
    WHERE A.BH = B.BH
    GROUP BY A.BM
      

  4.   

    方法一(分析函数 row_number()):
      select * from (select row_number() over(partition by bh order by bh) rn,a.* from a,b where a.bh=b.bh) where rn<=1;方法二(rowid): 
      select * from (select * from a,b where a.bh=b.bh) tmp where rowid in (select 
       max(rowid) from tmp group by bh);
      

  5.   

    select a.bh,a.bm,b.ry from a,b where a.bh=b.bh and rownum < 2;