aa  0003
aa  0004
aa  0005
bb  3112
bb  4221
bb  0012都是字符串,我想查询结果是
aa 0003
bb 3112

解决方案 »

  1.   

    create table #(name varchar(10),code varchar(10))
    insert into #
    select 'aa','0003'
    union all select 'aa','0004'
    union all select 'aa','0005'
    union all select 'bb','3112'
    union all select 'bb','4221'
    union all select 'bb','0012'select * from #
    where code=(select top 1 code from # t where t.name=#.name)drop table #
      

  2.   

    create table #(name varchar(10),code varchar(10))
    insert into #
    select 'aa','0003'
    union all select 'aa','0004'
    union all select 'aa','0005'
    union all select 'bb','3112'
    union all select 'bb','4221'
    union all select 'bb','0012'select * from # a
    where exists (select 1 from # where name=a.name and code>a.code)
      

  3.   

    if object_id('pubs..tb') is not null
       drop table tb
    gocreate table tb
    (
    a varchar(10),
    b varchar(10)
    )insert into tb(a,b) values('aa',  '0003')
    insert into tb(a,b) values('aa',  '0004')
    insert into tb(a,b) values('aa',  '0005')
    insert into tb(a,b) values('bb',  '3112')
    insert into tb(a,b) values('bb',  '4221')
    insert into tb(a,b) values('bb',  '0012')select * from tb b
    where b=(select top 1 b from tb where a=b.a)drop table tba          b          
    ---------- ---------- 
    aa         0003
    bb         3112(所影响的行数为 2 行)
      

  4.   

    select  a, min(b) from tb group by a
      

  5.   

    declare @t table (name varchar(10),code varchar(10))
    insert into @t
    select 'aa','0003'
    union all select 'aa','0004'
    union all select 'aa','0005'
    union all select 'bb','3112'
    union all select 'bb','4221'
    union all select 'bb','0012'select distinct name,code=(select top 1 code from @t where name=t.name) from @t t
    所影响的行数为 6 行)name       code       
    ---------- ---------- 
    aa         0003
    bb         3112(所影响的行数为 2 行)
      

  6.   

    select tb.* from tb where tb.id in (select top 1 tb1.id from tb tb1 where tb1.id=tb.id)
      

  7.   

    id 就是aa ,bb 那一列的列名,子查询需要加上排序才行