我有表,结构如下:
field1           field2
   a               a1
   a               a2
   a               a3
   b               a1
   b               a4
   c               a2
   .............
想得到如下的结果:
    a               a1
   a               a2
   a               a3
   b               a4就是field2不重复的,第一个出现在field1中的值。

解决方案 »

  1.   

    select * from tb a where not exists(select 1 from tb where field2=a.field2 and field1<a.field2)按你给的那些数据这样是可以的
    主要现在是不知道你FIELD1里面存放的数据结构是怎样的.
      

  2.   

    declare @t table(A varchar(10),B varchar(10))
    insert @t select   'a',              'a1' 
    insert @t select   'a',              'a2' 
    insert @t select    'a',              'a3' 
    insert @t select    'b',              'a1' 
    insert @t select    'b',              'a4' 
    insert @t select    'c',              'a2' 
    select * from @t t where not exists(select 1 from @t where a<t.a and b=t.b)A          B
    ---------- ----------
    a          a1
    a          a2
    a          a3
    b          a4
      

  3.   

    SELECT *,IDNO=IDENTITY(INT,1,1) INTO #T FROM TBselect * from #T a where not exists(select 1 from #T where field2=a.field2 and IDNO<a.IDNO)DROP TABLE #T
      

  4.   

    field1上有索引吗? 是如何排列的你怎么知道field1第一个出现的是 a还是b还是c
      

  5.   

    a和a1没关系,看来我表达的不清楚。
    field1          field2 
      time1              a1 
      time1              a2 
      time2              a3 
      time2              a1 
      time3              a4 
      time4              a2 
      ............. 
    想得到如下的结果: 
      time1              a1 
      time1              a2 
      time2              a3 
      time3              a4