按照A2 group by分组,想要查询结果

解决方案 »

  1.   


    按照A2 group by分组,想要查询结果
    那按照你给的数据。
    select min(A1),A2 from t group by A2
      

  2.   


    with test as (
    select 'a' as a1,1 as a2 from dual
    union all
    select 'b',1 from dual
    union all
    select 'c',1 from dual
    union all
    select 'd',1 from dual
    union all
    select 'e',2 from dual
    union all
    select 'f',2 from dual
    union all
    select 'g',2 from dual
    union all
    select 'h',3 from dual
    union all
    select 'i',3 from dual
    )
    select a1,a2 from (
    select a1,a2,row_number() over(partition by a2 order by a1) as rn from test
    ) where rn = 1;
      

  3.   


    按照A2 group by分组,想要查询结果
    那按照你给的数据。
    select min(A1),A2 from t group by A2

    如果A1字段是随机生成的字符串呢,我只想取第一个
      

  4.   


    按照A2 group by分组,想要查询结果
    那按照你给的数据。
    select min(A1),A2 from t group by A2

    如果A1字段是随机生成的字符串呢,我只想取第一个
    5#的可以的
      

  5.   

    楼主的意思很简单就是以列A2分组查询每组第一条记录
    select A1,A2
      from (select t1.*, row_number() over(partition by A2 order by rownum) cn
              from t1)
     where cn = 1;
    结果:
    A1         A2
    -- ----------
    a           1
    e           2
    h           3