简单的举个例子,表结构是id 和name
例如:id ,name
      1   aaa
      2   bbb
      3   ccc
      4   aaa
      5   ccc
      6   ccc
     ...  ....这里要统计出name出现10次以上的有几个,请问该如何写这个统计SQL??

解决方案 »

  1.   

    select * from tab
    group by name having(count(*)) >10
      

  2.   

    select tn.* from tablename tn
    group by tn.name having(count(tn.name)) >10
      

  3.   

    谢谢,如果我想再进一步,直接算出name数量大于10的总共有多少怎么弄?
    我这样写不对,select count(select name from tab
    group by name having(count(*)) >10) from tab,该怎么写比较好?
      

  4.   

    select count(name) from tab group by name having(count(*)) >10你那样写,效率不高。。
      

  5.   

    select count(*) from tab
    group by name having(count(*)) >10
      

  6.   

    select sum(a.num)
    from(
        select tn.name, count(tn.name) num from tablename tn
        group by tn.name having(count(tn.name)) >10
    )a;
      

  7.   

    select count(*)
    from(
        select name
        from 表结构
        group by name
        having count(*) >= 10
    ) t;
      

  8.   

    问题说明越详细,回答也会越准确!参见如何提问。(提问的智慧
         (不要高估你的汉语表达能力或者我的汉语理解能力)
       建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
       参考一下这个贴子的提问方式http://topic.csdn.net/u/20091130/20/8343ee6a-417c-4c2d-9415-fa46604a00cf.html
       
       1. 你的 create table xxx .. 语句
       2. 你的 insert into xxx ... 语句
       3. 结果是什么样,(并给以简单的算法描述)
       4. 你用的数据库名称和版本(经常有人在MS SQL server版问 MySQL)
       
       这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。   
      

  9.   

    恭喜升星了!select count(*) from
    (
    select name from tb group by name having(count(1)>10)
    ) x