数据表A:
字段1     字段2     字段3
A1      0001       5
A1      0002       4
A2      0001       5
A2      0002       4
A3      0001       5
A3      0002       4
A4      0003       8
A5      0003       8
...
想得到表B:
字段1     字段2     字段3
A1      0001       5
A1      0002       4
A4      0003       8
...
需要用比较动态的sql语句?求教!谢谢!

解决方案 »

  1.   

    select * from tb t where 字段1=(select min(字段1) from tb where 字段2=t.字段2)
      

  2.   

    create table tb (c1 varchar(10),c2 varchar(10),c3 int)
    insert into tb select 'A1','0001',5
    insert into tb select 'A1','0002',4
    insert into tb select 'A2','0001',5
    insert into tb select 'A2','0002',4
    insert into tb select 'A3','0001',5
    insert into tb select 'A3','0002',4
    insert into tb select 'A4','0003',8
    insert into tb select 'A5','0003',8
    go
    select * from tb a where not exists(select 1 from tb where c2=a.c2 and c1<a.c1)
    /*
    c1         c2         c3
    ---------- ---------- -----------
    A1         0001       5
    A1         0002       4
    A4         0003       8(3 行受影响)*/
    go
    drop table tb