今天下午求帮助,得到大虾们的热心帮助,谢谢各位大虾!下面是原贴中的问题:有表TB1,字段a,b,c,d,表中存在大量重复数据,现在想从表中提取部分数据插入TB2中,条件是:其重复的次数在前10名,即:提取出来的数据只有10条,并且它们在TB1中重复的次数排在所有数据重复次数的前10名.请大虾们帮我一下,先谢谢!在运用时我发现一个问题,如果TB1中有如下数据a b c d
1 1 1 1
1 1 1 1
1 1 1 1
2 2 2 2
2 2 2 2
2 2 2 2
3 3 3 3
3 3 3 3
4 4 4 4
4 4 4 4
5 5 5 5
5 5 5 5
6 6 6 6运行命令
select top 3 A,B,C,D
from TB1
group by A,B,C,D
order by count(*) desc结果是
a b c d
1 1 1 1
2 2 2 2
5 5 5 5而我想要的结果是,要么3、4、5都保留,要么3、4、5都不要,请大虾们帮帮我。谢谢!

解决方案 »

  1.   

    insert into tb2
    select m.* from tb1 m where exists(select 1 from (select top 10 a,b,c,d,count(1) cnt from tb1 group by a, b, c, d order by cnt desc) n
    where n.a = m.a and n.b = m.b and n.c = m.c and n.d = m.d
    )
      

  2.   

    select top 3 A,B,C,D
    from TB1
    WHERE A NOT IN (3,4,5)
    group by A,B,C,D
    order by count(*) desc 
      

  3.   

    ----------------------------------------------------------------
    -- Author  :fredrickhu(我是小F,向高手学习)
    -- Date    :2009-12-25 16:48:03
    -- Version:
    --      Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86) 
    -- Nov 24 2008 13:01:59 
    -- Copyright (c) 1988-2005 Microsoft Corporation
    -- Developer Edition on Windows NT 5.2 (Build 3790: Service Pack 1)
    --
    ----------------------------------------------------------------
    --> 测试数据:[tb]
    if object_id('[tb]') is not null drop table [tb]
    go 
    create table [tb]([a] int,[b] int,[c] int,[d] int)
    insert [tb]
    select 1,1,1,1 union all
    select 1,1,1,1 union all
    select 1,1,1,1 union all
    select 2,2,2,2 union all
    select 2,2,2,2 union all
    select 2,2,2,2 union all
    select 3,3,3,3 union all
    select 3,3,3,3 union all
    select 4,4,4,4 union all
    select 4,4,4,4 union all
    select 5,5,5,5 union all
    select 5,5,5,5 union all
    select 6,6,6,6
    --------------开始查询--------------------------
    select 
     top 3  with ties a,b,c,d
    from
     (select a,b,c,d,count(1) as num from tb group by a,b,c,d)t
    order by num desc----------------结果----------------------------
    /* a           b           c           d
    ----------- ----------- ----------- -----------
    1           1           1           1
    2           2           2           2
    3           3           3           3
    4           4           4           4
    5           5           5           5(5 行受影响)
    */
      

  4.   

    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb] (a int,b int,c int,d int)
    insert into [tb]
    select 1,1,1,1 union all
    select 1,1,1,1 union all
    select 1,1,1,1 union all
    select 2,2,2,2 union all
    select 2,2,2,2 union all
    select 2,2,2,2 union all
    select 3,3,3,3 union all
    select 3,3,3,3 union all
    select 4,4,4,4 union all
    select 4,4,4,4 union all
    select 5,5,5,5 union all
    select 5,5,5,5 union all
    select 6,6,6,6
    select top 3 with ties A,B,C,D 
    from TB 
    group by A,B,C,D 
    order by count(*) desc 
    /*
    A           B           C           D
    ----------- ----------- ----------- -----------
    1           1           1           1
    2           2           2           2
    3           3           3           3
    4           4           4           4
    5           5           5           5
    */
      

  5.   

    ----------------------------------------------------------------
    -- Author  :fredrickhu(我是小F,向高手学习)
    -- Date    :2009-12-25 16:48:03
    -- Version:
    --      Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86) 
    -- Nov 24 2008 13:01:59 
    -- Copyright (c) 1988-2005 Microsoft Corporation
    -- Developer Edition on Windows NT 5.2 (Build 3790: Service Pack 1)
    --
    ----------------------------------------------------------------
    --> 测试数据:[tb]
    if object_id('[tb]') is not null drop table [tb]
    go 
    create table [tb]([a] int,[b] int,[c] int,[d] int)
    insert [tb]
    select 1,1,1,1 union all
    select 1,1,1,1 union all
    select 1,1,1,1 union all
    select 2,2,2,2 union all
    select 2,2,2,2 union all
    select 2,2,2,2 union all
    select 3,3,3,3 union all
    select 3,3,3,3 union all
    select 4,4,4,4 union all
    select 4,4,4,4 union all
    select 5,5,5,5 union all
    select 5,5,5,5 union all
    select 6,6,6,6
    --------------开始查询--------------------------
    select top 3 with ties A,B,C,D 
    from tb 
    group by A,B,C,D 
    order by count(*) desc 
    ----------------结果----------------------------
    /* a           b           c           d
    ----------- ----------- ----------- -----------
    1           1           1           1
    2           2           2           2
    3           3           3           3
    4           4           4           4
    5           5           5           5(5 行受影响)
    */