表t
ID  Name  Type  Detail
1   a     1     aaa
2   a     2     bbb
3   b     2     bbb
要求:Name相同的就显示Type小的那条记录
比如条件是空的
出来的结果是
ID  Name  Type  Detail
1   a     1     aaa
3   b     2     bbb

解决方案 »

  1.   

    select a.* from 表t a where not exists(select 1 from 表t where Name=a.Name and Type<a.Type)
      

  2.   

    select * from 表t as t
    where Type = (select min(Type) from 表t where Name=t.Name)
      

  3.   

    select a.* from t a,
    (select name,min(type) type from t group by name) b
    where a.name = b.name and a.type = b.type
      

  4.   

    select a.* from 表t a where a.Type=(select min(Type) from 表t where Name=a.Name)select a.* from 表t a where a.Type=(select top 1 Type from 表t where Name=a.Name order by Type)select a.* from 表t a where a.Type=(select top 1 ID from 表t where Name=a.Name order by Type)
      

  5.   

    select * from #t a where not exists( select 1 from #t where [name]=a.[name] and 
    type<a.type)
      

  6.   

    select * from t where type in (select min(type) from t group by name)
      

  7.   

    select a.* from t a where a.type = (select top 1 type from t where name = a.name)
      

  8.   

    select a.* from 表t a
    left join  (select Name,min(type) as type from 表t group by name) b
    on a.name=b.name and a.type=b.type
      

  9.   

    没事测试了一下,libin_ftsafe(子陌红尘:TS for Banking Card) 写的完全正确:)
    create table t1(
      ID int identity(1,1),
      Name varchar(1),
      Type int,
      Detail varchar(10)
    )insert t1
    select 'a',1,'aaa' union all
    select 'a',2,'bbb' union all
    select 'b',2,'bbb'select * from t1 a
    where not exists(select 1 from t1 where Name=a.Name and Type<a.Type )
    --result:
    1   a 1 aaa
    3   b 2 bbb
      

  10.   

    create table t (ID int, Name varchar(10),Type int, Detail varchar(10))
    insert into t values(1,   'a',     1,     'aaa')
    insert into t values(2,   'a',     2,     'bbb')
    insert into t values(3,   'b',     2,     'bbb')
    go
    select a.* from t a,
    (select name,min(type) type from t group by name) b
    where a.name = b.name and a.type = b.type
    /*
    ID          Name       Type        Detail     
    ----------- ---------- ----------- ---------- 
    1           a          1           aaa
    3           b          2           bbb
    (所影响的行数为 2 行)
    */
    select a.* from t a where a.type = (select top 1 type from t where name = a.name)
    /*
    ID          Name       Type        Detail     
    ----------- ---------- ----------- ---------- 
    1           a          1           aaa
    3           b          2           bbb
    (所影响的行数为 2 行)
    */
    drop table t
      

  11.   

    select a.* from 表t a where not exists(select 1 from 表t where Name=a.Name and Type<a.Type)select a.* from 表t a where a.Type=(select top 1 Type from 表t where Name=a.Name order by Type)测试了一下,第一条效率高好多
      

  12.   

    select ID,Name,Min(Type),Detail from 表abc Group by name速度不知..
      

  13.   

    select ID,Name,Min(Type),Detail  from a  group by name