A    B    C
1    1    1
1    2    2
2    1    3
2    2    4
distinct A以后,B和C的值你如何关联?
逻辑上讲不同的,除非如下
A    B    C
1    1    1
1    1    1
2    2    2
2    2    2
那么distinct a,b,c
可以留下两条纪录

解决方案 »

  1.   

    first_value()实现同值的第一条记录
      

  2.   

    SQL> create table test(a number,b number,c number);Table createdSQL> insert into test select 1,1,1 from dual
      2  union all select 1,1,1 from dual
      3  union all select 2,1,3 from dual
      4  union all select 2,2,4 from dual;4 rows insertedSQL> select * from test;         A          B          C
    ---------- ---------- ----------
             1          1          1
             1          1          1
             2          1          3
             2          2          4SQL> select a,max(b) b,max(c) c from test 
      2  group by a;         A          B          C
    ---------- ---------- ----------
             1          1          1
             2          2          4SQL>
      

  3.   

    或者
    SQL> select a,b,c from (select a,b,c,row_number() over(partition by a order by a) rd from test) where rd=1;         A          B          C
    ---------- ---------- ----------
             1          1          1
             2          1          3
      

  4.   

    select 字段A,字段B,字段C,字段D,... from 表名 right join (select distinct 字段A from 表名 where 字段B = 某值) tb1 on 表名.字段A=tb1.字段A