比如在 [table] 表中要过滤 [name] 字段重复的记录,并且随即保留一条。下面这个语句只能保留[id]最大的一条记录,如果想随即保留一条怎么办呢??  高人指点~~~~select * from [table] where [id] in (select max([id]) from [table] group by [name])

解决方案 »

  1.   

    select * from [table] a,(select max([id]) as id from [table] group by [name]) b
    where a.id=b.id and a.name=b.name
      

  2.   

    http://topic.csdn.net/u/20080626/00/43d0d10c-28f1-418d-a05b-663880da278a.html?73339
      

  3.   

    方法1:
    Select * from #T a where not exists(select 1 from #T where Name=a.Name and ID>a.ID)方法2:
    select a.* from #T a join (select max(ID)ID,Name from #T group by Name) b on a.Name=b.Name and a.ID=b.ID order by ID方法3:
    select * from #T a where ID=(select max(ID) from #T where Name=a.Name) order by ID
      

  4.   

    select * from [table] where [id] in (select max([id]) from [table] group by [name])
    select * from [table] where [id] in (select MIN([id]) from [table] group by [name])
      

  5.   

    select * from [table] a,(select name,max([id]) as id from [table] group by [name]) b 
    where a.id=b.id and a.name=b.name   --相同NAME取ID最大select * from table a 
    not exists(select 1 from table where name=a.name and id>a.id  --相同NAME取ID最大select * from [table] a,(select name,min([id]) as id from [table] group by [name]) b 
    where a.id=b.id and a.name=b.name   --相同NAME取ID最小
    select * from table a 
    not exists(select 1 from table where name=a.name and id<a.id  --相同NAME取ID最小试下先!
      

  6.   

    ;with t as

      select rn=row_number()over(partition by name order by newid()),* from tb t where exists(select 1 from tb where name=t.name and id<>t.id)
    )
    select * from t where rn=1
      

  7.   

    --2000
      select * 
      from tb t 
      where exists(select 1 from tb where name=t.name and id<>t.id)
        AND id=(  
    select TOP 1 id 
    from tb a 
    where exists(select 1 from tb where name=a.name and id<>a.id)
    ORDER BY NEWID())
      

  8.   


    用8楼TONY哥的 
    如果是2000的话需要加临时表了 
      

  9.   


    declare @t table(id int identity,name varchar(2))
    insert @t select 
    'A' union all select
    'A' union all select
    'A' union all select
    'E' union all select
    'E' union all select
    'E' union all select
    'G' union all select
    'G' union all select
    'G' union all select
    'E'----sql2005
    ;with szy as
    (
    select *,px=row_number()over(partition by name order by newid())
    from @t
    )
    select * from szy where px=1id          name px
    ----------- ---- --------------------
    3           A    1
    6           E    1
    8           G    1(3 行受影响)
      

  10.   

    环境不允许啊有没有直接点的SQL语句呢,高人开动起来啊~~~~
      

  11.   

    select distinct [name],[其他字段] from [table]
    [name] 相同的,数据也应该一样吧,如果不一样的话,上面那条语句会把不同的都显示出来。
      

  12.   

      select * 
      from tb t 
      where exists(select 1 from tb where name=t.name and id<>t.id)
        AND id=(  
            select TOP 1 id 
            from tb a 
            where exists(select 1 from tb where name=a.name and id<>a.id)
              and t.name=a.name--少个条件,这样试试
            ORDER BY NEWID())
      

  13.   

    drop table test;
    --先求出符合条件的记录:
    create table test(col1 int identity(1,1), col2 varchar(10), col3 varchar(10));insert into test(col2, col3)
    select '我要银子','我没有' UNION ALL
    select '我要银子','你有没?' UNION ALL
    select '我要金子','金子涨价了' UNION ALL
    select '多要银元','我没有' UNION ALL
    select '我要银子','我没有' UNION ALL
    select '我要银子', '你有没?' UNION ALL
    select '我要银子', '明年才有' UNION ALL
    select '我要银子', '你有没?'select * from test;
    select distinct col2, col2 from test;
    select t1.all_sum as '所有记录行',
           t2.dis_sum as '无重复记录行', 
           t1.all_sum-t2.dis_sum as '将要删除的行数'
    from (select count(*) as all_sum from test ) t1,
    (select count(*) as dis_sum from (select distinct col2, col3 from test) t) t2;
    ---如:随机删除 col2='我要银子'  的重复记录
    select sum(row_sum) as all_del from ( 
    select col2, col3, count(col1)-1 as 'row_sum'
     from test
    where col2='我要银子'
    group by col2, col3
    having count(col1)-1<>0) t1--删除条件:将col2、col3都相同的,视为重复记录,将其随机删除只剩一行
    --选择语句:
    select top (select sum(row_sum) as all_del from ( 
                 select col2, col3, count(col1)-1 as 'row_sum'
                from test
                where col2='我要银子'
                group by col2, col3
                having count(col1)-1<>0) t1) 
        t1.col1, t1.col2, t1.col3 
        from test t1 where t1.col2='我要银子' and t1.col1 not in 
      (select col1 from test t3 where exists ( 
                         select col2, col3 
                         from test where col2='我要银子'
                         and col2=t3.col2 and col3=t3.col3 
                         group by col2, col3 having count(col1)=1 ))
    order by NEWID()
    --删除语句:
    DELETE FROM test
    WHERE col1 in ( select col1 from (
    select top (select sum(row_sum) as all_del from ( 
                 select col2, col3, count(col1)-1 as 'row_sum'
                from test
                where col2='我要银子'
                group by col2, col3
                having count(col1)-1<>0) t1) 
        t1.col1, t1.col2, t1.col3 
        from test t1 where t1.col2='我要银子' and t1.col1 not in 
      (select col1 from test t3 where exists ( 
                         select col2, col3 
                         from test where col2='我要银子'
                         and col2=t3.col2 and col3=t3.col3 
                         group by col2, col3 having count(col1)=1 ))
    order by NEWID()) t
    )
      

  14.   

    drop table test;
    --先求出符合条件的记录:
    create table test(col1 int identity(1,1), col2 varchar(10), col3 varchar(10));insert into test(col2, col3)
    select '我要银子','我没有' UNION ALL
    select '我要银子','你有没?' UNION ALL
    select '我要金子','金子涨价了' UNION ALL
    select '多要银元','我没有' UNION ALL
    select '我要银子','我没有' UNION ALL
    select '我要银子', '你有没?' UNION ALL
    select '我要银子', '明年才有' UNION ALL
    select '我要银子', '你有没?'select * from test;
    select distinct col2, col2 from test;
    select t1.all_sum as '所有记录行',
           t2.dis_sum as '无重复记录行', 
           t1.all_sum-t2.dis_sum as '将要删除的行数'
    from (select count(*) as all_sum from test ) t1,
    (select count(*) as dis_sum from (select distinct col2, col3 from test) t) t2;
    ---如:随机删除 col2='我要银子'  的重复记录
    select sum(row_sum) as all_del from ( 
    select col2, col3, count(col1)-1 as 'row_sum'
     from test
    where col2='我要银子'
    group by col2, col3
    having count(col1)-1<>0) t1--删除条件:将col2、col3都相同的,视为重复记录,将其随机删除只剩一行
    --选择语句:
    select top (select sum(row_sum) as all_del from ( 
                 select col2, col3, count(col1)-1 as 'row_sum'
                from test
                where col2='我要银子'
                group by col2, col3
                having count(col1)-1<>0) t1) 
        t1.col1, t1.col2, t1.col3 
        from test t1 where t1.col2='我要银子' and t1.col1 not in 
      (select col1 from test t3 where exists ( 
                         select col2, col3 
                         from test where col2='我要银子'
                         and col2=t3.col2 and col3=t3.col3 
                         group by col2, col3 having count(col1)=1 ))
    order by NEWID()
    --删除语句:
    DELETE FROM test
    WHERE col1 in ( select col1 from (
    select top (select sum(row_sum) as all_del from ( 
                 select col2, col3, count(col1)-1 as 'row_sum'
                from test
                where col2='我要银子'
                group by col2, col3
                having count(col1)-1<>0) t1) 
        t1.col1, t1.col2, t1.col3 
        from test t1 where t1.col2='我要银子' and t1.col1 not in 
      (select col1 from test t3 where exists ( 
                         select col2, col3 
                         from test where col2='我要银子'
                         and col2=t3.col2 and col3=t3.col3 
                         group by col2, col3 having count(col1)=1 ))
    order by NEWID()) t
    )