数据表table里有这样的字段a,b
a        b  a1       b1
a2       b2
a3       b1
a4       b3
我想通过一条sql语句将字段b里重复的数据只显示一次,得出的结果为
a        b  a1       b1
a2       b2
a4       b3
我应该怎么写sql语句呢,用select distinct b from table  结果,字段a的值没有显示出来,我要求两字段都能显示出来的,希望大家帮忙写下,谢谢

解决方案 »

  1.   

    select * from table where b in (select distinct b from table  )
      

  2.   

    a1      b1 
    a3      b1 
    任意显示一条?
      

  3.   

    SELECT * FROM table WHERE b in (SELECT b FROM table GROUP BY b)
      

  4.   

    还是cc_net 问的好?
    a1      b1 
    a3      b1 
    你选哪一条?
    如果如你上面的结果:
    a1      b1 
    a2      b2 
    a4      b3 根本不用什么distinct.直接group by好了。
    select * from table group by b 
      

  5.   

    Select max(a),max(b)
    from tbl
    group by b
      

  6.   

    根本不用什么distinct.直接group by好了。 
    select * from table group by b   这一句语句本身就是错的啊
    要这样写select b from table group by b  可这样,又只查出一列,字段a哪一列没显示出来的
      

  7.   

    select * from (select a,b,row_number over(partition by b ordre by a)[order] from table)A  where A.order<2
     
    可以吗?
      

  8.   

    可用存储过程
    用游标select distinct b from table1
    循环查询
    insert into #ls select top 1 * from table1 where b='b1'
      

  9.   

    select * from (select a,b,row_number() over(partition by b order by a)[order] from [Table])A  where A.[order] <2
    刚才写错了,你试一下这个,可以吗?
      

  10.   

    select max(a),max(b) from table group by b