id       seq_no   value
1 2 B
1 1 A
2 1 C
3 3 F
3 2 E
3 1 D
4 1 G现在我想获得id对应的seq_no最大的那行,请问如何获取?

解决方案 »

  1.   

    select*
    from (select id,max(seq_no)as seq_no
         from tb
         group by id)a
    join tb b on a.id=b.id and b.seq_no=a.seq_no
      

  2.   

    select * from 表名 A where not exists (select 1 from 表名 where seq_no>A.seq_No)
      

  3.   

    select id,max(seq_no) as seq_no  from t group by id?
      

  4.   

    select id, seq_no=max(seq_no) from tbName group by id
      

  5.   

    select * from table t where not exists (select * from table where id=t.id and seq_no>t.seq_No)
      

  6.   

    create table T(id int, seq_no int, value char(1))
    insert T select 1, 2, 'B'
    union all select 1, 1, 'A'
    union all select 2, 1, 'C'
    union all select 3, 3, 'F'
    union all select 3, 2, 'E'
    union all select 3, 1, 'D'
    union all select 4, 1, 'G'
    --1
    select id, max(seq_no) as seq_no from t group by id--2
    select * from T as A
    where not exists(select 1 from T where id=A.id and seq_no>A.seq_no)