有表 a
序号  编号  类别  值  
====================
1    1001  1    abc
1    1001  2    def
1    1001  3    val
2    1001  1    cad
2    1001  2    cap
2    1001  3    oop
3    2001  1    aaa
3    2001  2    add
3    2001  3    7yr
4    2001  1    443
4    2001  2    400
4    2001  3    50f如何得到如下结果(所有不同"编号"且类别为"2"的每一第一条记录,序号可以不要)
序号  编号  类别  值  
====================
1    1001  2    def
3    2001  2    add
等待中...

解决方案 »

  1.   


    SELECT 序号,  编号,  类别 , 值 FROM
    (select row_number() over(partition by 编号 order by 类别) rn,A.* FROM A)
    WHERE RN=1
      

  2.   

    create table test(序号 varchar(20),编号 varchar(20),类别 varchar(20),值 varchar(20));
    insert into test select '1','1001','1','abc' from dual;
    insert into test select '1','1001','2','def' from dual;
    insert into test select '1','1001','3','val' from dual;
    insert into test select '2','1001','1','cad' from dual;
    insert into test select '2','1001','2','cap' from dual;
    insert into test select '2','1001','3','oop' from dual;
    insert into test select '3','2001','1','aaa' from dual;
    insert into test select '3','2001','2','add' from dual;
    insert into test select '3','2001','3','7yr' from dual;
    insert into test select '4','2001','1','443' from dual;
    insert into test select '4','2001','2','400' from dual;
    insert into test select '4','2001','3','50f' from dual;
     
    --测试
    select * from(
    select a.*,row_number()over(partition by 编号 order by  序号) as rn from test a) where rn=1;drop table test;--
    /*序号 编号 类别 值 RN
    1 1001 1 abc 1
    3 2001 1 aaa 1*/
      

  3.   

    SQL> select 序号, 编号, 类别, 值
      2    from (select 序号,
      3                 编号,
      4                 类别,
      5                 值,
      6                 row_number() over(partition by 编号 order by rowid) rn
      7            from a
      8           where 类别 = 2)
      9   where rn = 1;
     
    序号                 编号                 类别                 值
    -------------------- -------------------- -------------------- --------------------
    1                    1001                 2                    def
    3                    2001                 2                    add