我想取出多条记录中某的一条记录(条件是其中一个字段值如果相同则只取其中一条)。
如:
  ID , Field1,  Field2
  1 ,  aaa   ,  123
  2 ,  aaa   ,  234
想要实现最后的结果
  2,aaa,234
请指教

解决方案 »

  1.   

    select * from table1 t where exists(select 1 from table1 where t.Field1=Field1 and t.Field2>Field1)
      

  2.   

    select * from 表 a where not exist(select 1 from 表 b where a.name=b.name and b.id<a.id)
      

  3.   


    create table ##p(field1 varchar(50),field2 varchar(50))insert into ##p select 'aaa','123'
    union all select 'aaa','234'
    union all select 'bbb','234'
    union all select 'ccc','23'
    union all select 'bbb','444'
    /*相同的纪录取Field2最大的*/
    select Field1, max(Field2) from ##p group by field1
    /*相同的纪录取Field2最小的*/
    select Field1, min(Field2) from ##p group by field1drop table ##p
      

  4.   

    我用这种方法不知道好不好,还请高手指教
    SELECT a.*
    FROM Table1 a INNER JOIN
          Table1 b ON a.Field1 = b.Field1 AND b.ID< a.ID