我需要查询结果是001     001        ibm
002     00001      sun还有这个结果不清晰,为什么是002 00001 sun 而不要002 002 sun.

解决方案 »

  1.   

    根据你的语句
    002    002      sun
    002    00001    sun
    这两个数据的code1(002) 都在B表中的code1中,所以这两条记录都会被查询出来
      

  2.   

    结果就是这样,从我现在程序角度,因为我想要A表中在B表中有的数据只呈现出一条就行了,不然数据会很多。而第二条是最好的
    ------------------------
    我需要查询结果是001     001        ibm
    002     00001      sun还有这个结果不清晰,为什么是002 00001 sun 而不要002 002 sun.
      

  3.   

    try:select * from A where code1  in (select code1   from B ) and rowid in (select max(rowid) from B group by code1,name)
      

  4.   

    sorry,前面打错字了select * from A where code1  in (select code1   from B ) and rowid in (select max(rowid) from A group by code1,name)
      

  5.   

    不行我的A表是一个视图,其中应用了DISTINCT 语句的
      

  6.   

    002    002         sun
    002    00001       sun
    这两行的区别仅仅是行号不同吗,‘002’和‘00001’的选择有什么方法吗
      

  7.   

    try:select code1,code2,name from (select rownum num,* from A where code1  in (select code1   from B )) T where num in (select max(num) from (select rownum num,* from A where code1  in (select code1   from B )) T group by code1,name
      

  8.   

    retry:select code1,code2,name from (select rownum num,code1,code2,name from A where code1 in (select code1 from B )) T where num in (select max(num) from (select rownum num,code1,code2,name from A where code1 in (select code1 from B )) T group by code1,name);
      

  9.   

    select code1,code2,name from
    (
     select aa.code1,aa.code2,aa.name,row_number()
     over (partition by aa.code1 order by aa.code2) top1 from aa,bb where aa.code1=bb.code1
    )
    where top1 <= 1
    /
      

  10.   

    SQL> select * from aa;CODE1    CODE2    NAME
    -------- -------- --------
    001      001      IBM
    002      002      SUN
    002      00001    SUN
    003      003      LEGENDSQL> select * from bb;CODE1    DATESTR
    -------- --------
    001      A
    002      BSQL> select code1,code2,name from
      2  (
      3   select aa.code1,aa.code2,aa.name,row_number()
      4   over (partition by aa.code1 order by aa.code2) top1 
      5   from aa,bb where aa.code1=bb.code1
      6  )
      7  where top1 <= 1
      8  /CODE1    CODE2    NAME
    -------- -------- --------
    001      001      IBM
    002      00001    SUN
      

  11.   

    也可以这样
    select a.code1,a.code2,a.name from aa a,
    (select max(x.rowid) myrowid from aa x,bb y
    where x.code1=y.code1 group by x.code1) b
    where a.rowid=b.myrowid
    /