有如下内容的表: 
字段1 字段2 字段3 
A a1 1 
A a2 2 
B b1 3 
B b2 4 
B b3 5 
C c1 6 
C c2 7 
求一sql语句,以字段1分组,显示第一条记录。
结果如下:
字段1 字段2 字段3 
A a1 1 
B b1 3 
C c1 6 

解决方案 »

  1.   

    select * from table where (字段1,字段3) in
    (select 字段1,min(字段3) from table group by 字段1)
      

  2.   

    select distinct 字段1,字段2,字段3 from table order by 字段1;
      

  3.   

    select a, b, c
      from (select a, b, c, row_number() over(partition by a order by c) rec
              from t
             group by a, b, c)
     where rec = 1:)
      

  4.   

    select 字段1,min(字段2),min(字段3) from table group by 字段1
      

  5.   

    3楼是对的.
    楼住你是如何确定第1条记录的?
    3楼的是按照c排序的.
    其他人的sql是寻找最小的c,如果排列顺序不是按照c递增的话就不对了.不过数据如果如楼住所列也是可以的
      

  6.   

    SQL> with a as(select 'a' id,'a1' id2, 1 id3 from dual
      2            union
      3            select 'a' id,'a2' id2, 2 id3 from dual
      4            union
      5            select 'b' id,'b1' id2, 3 id3 from dual
      6            union
      7            select 'b' id,'b2' id2, 4 id3 from dual
      8            union
      9            select 'b' id,'b3' id2, 5 id3 from dual
     10            union
     11            select 'c' id,'c1' id2, 6 id3 from dual
     12            union
     13            select 'c' id,'c2' id2, 7 id3 from dual
     14            )
     15   select id,id2,id3 from  (select  id,id2,id3, row_number()over(partition by id order by id) rn
     16  from a)
     17  where rn=1
     18  ;
     
    ID ID2        ID3
    -- --- ----------
    a  a1           1
    b  b1           3
    c  c1           6
      

  7.   

    select 字段1,
    min(字段2) keep(DENSE_RANK LAST ORDER BY rowid) 字段3,
    min(字段3) keep(DENSE_RANK LAST ORDER BY rowid) 字段3
    from T
    group by A
      

  8.   

    楼住你首先要确定一点
    字段1   字段2   字段3   
    A   a1   1   
    A   a2   2  
    这2条记录,你是按照什么排序得出哪条记录是第1条记录的?
    确定下来就好写了.
    如果想要任意条数据,上面的都是正确的