用sql过滤重复记录,遇到一个问题需要过滤某一个字段的值重复的记录,但要取一条出来。只有一个字段值是重复的,其它字段值不重复。我用这样的语句来做Select * From [tb_SurveyData] where Sd_Id in (select max(Sd_Id) as Sd_Idd from [tb_SurveyData] group by Sd_Ip) And Sd_Sid=@Sd_Sid可以复到的结果不是我想要的,似乎每次只得到一个结果。为什么?

解决方案 »

  1.   

    Sql中的DISTINCT函数.
    查看Sql帮助.
      

  2.   

    你用max()肯定只取到最大的那条记录了.如果要取不重复的,应该用distinct
    select * from [tb_SurveyData] where Sd_Id in (select distinct Sd_Id from [tb_SurveyData] group by Sd_Ip)
      

  3.   


    create table table1 (id int, ip varchar(15))
    insert table1 
    select 1, '127.0.0.1' union
    select 2, '127.0.0.2' union
    select 3, '127.0.0.2' union
    select 4, '127.0.0.3' union
    select 5, '127.0.0.3' union
    select 6, '127.0.0.4' union
    select 7, '127.0.0.4' -- 查询
    select * from table1 where id in -- 检索重复ip的纪录
    (select min(id) from table1 where ip in -- 求重复ip的其中一个id
    (select ip from table1 group by ip having count(1) > 1) -- 取出重复的ip
    group by ip
    )drop table table1
      

  4.   

    create table table1 (id int, ip varchar(15))
    insert table1 
    select 1, '127.0.0.1' union
    select 2, '127.0.0.2' union
    select 3, '127.0.0.2' union
    select 4, '127.0.0.3' union
    select 5, '127.0.0.3' union
    select 6, '127.0.0.4' union
    select 7, '127.0.0.4' -- 查询
    select distinct(ip) from table1drop table table1
      

  5.   

    Thank You ~~!!!!!!!!!!!!!!!!!
      

  6.   

    Select distinct 你要过滤重复记录的ColumnName From [tb_SurveyData] where Sd_Id in (select max(Sd_Id) as Sd_Idd from [tb_SurveyData] group by Sd_Ip) And Sd_Sid=@Sd_Sid 
      

  7.   

    select  *  from 数据表  where 需要过滤的重复列的列名 in( select distinct( 需要过滤的重复列的列名) from
     数据表  where 条件语句)
      

  8.   

    select * from 数据表 where 需要过滤的重复列的列名 in( select distinct( 需要过滤的重复列的列名) from
     数据表 where 判断最大值的列名 in(select max(判断最大值的列名 ) from 数据表  where 条件))