ID        列2
1        aaaaaaa
2        bbbbbb
3        cccc
4        aaaaaaa
5        cccc
6        ddddddd我要distinct列2,并且得到的id是第一个的值
即得到
1        aaaaaaa
2        bbbbbb
3        cccc
6        ddddddd

解决方案 »

  1.   

    select min(ID) as ID,列2
    from 表名
    group by 列2
      

  2.   

    select min(ID) as ID,        列2 from 表 group by  列2
      

  3.   

    create table test(ID int,col varchar(20))
    insert test select 1,'aaaaaaa'
    union all select 2,'bbbbbb'
    union all select 3,'cccc'
    union all select 4,'aaaaaaa'
    union all select 5,'cccc'
    union all select 6,'ddddddd'select * from test a where ID=
    (
    select top 1 ID from test where col=a.col
    )drop table testID          col                  
    ----------- -------------------- 
    1           aaaaaaa
    2           bbbbbb
    3           cccc
    6           ddddddd(所影响的行数为 4 行)
      

  4.   

    create table test(ID int,col varchar(20))
    insert test select 1,'aaaaaaa'
    union all select 2,'bbbbbb'
    union all select 3,'cccc'
    union all select 4,'aaaaaaa'
    union all select 5,'cccc'
    union all select 6,'ddddddd'select * from testselect min(id) id,col from test group by col
      

  5.   

    select min(ID) as ID,列2
    from 表名
    group by 列2