select
    distinct a.*
from 
    表 a
where
    f2=(select top 1 f2 from 表 where f1=a.f1)

解决方案 »

  1.   

    --以下SQL语句只针对当前数据有效,如果数据具有不确定性,建议使用临时表:
    create table #T(f1 int,f2 int,f3 int)
    insert into #T select 1,11,8
    insert into #T select 1,22,6
    insert into #T select 2,22,9
    insert into #T select 2,33,5
    insert into #T select 2,22,9
    insert into #T select 2,66,1
    insert into #T select 3,55,6
    insert into #T select 3,44,8select
        distinct a.*
    from 
        #T a
    where
        f2=(select top 1 f2 from #T where f1=a.f1)/*
    1    11    8
    2    22    9
    3    55    6
    */drop table #T
      

  2.   

    --借助临时表的实现:
    create table #T(f1 int,f2 int,f3 int)
    insert into #T select 1,11,8
    insert into #T select 1,22,6
    insert into #T select 2,22,9
    insert into #T select 2,33,5
    insert into #T select 2,22,9
    insert into #T select 2,66,1
    insert into #T select 3,55,6
    insert into #T select 3,44,8select identity(int,1,1) as id,* into #T1 from #Tselect
        a.f1,a.f2,a.f3
    from 
        #T1 a
    where
        not exists(select 1 from #T1 where f1=a.f1 and id<a.id)/*
    1    11    8
    2    22    9
    3    55    6
    */drop table #T,#T1
      

  3.   

    可以实现,但是效率可能会比较低select distinct (select top 1 f1 from 表 where f1=a.f1) f1,
                    (select top 1 f2 from 表 where f1=a.f1) f2,
                    (select top 1 f3 from 表 where f1=a.f1) f3
    from 表 a