small_id         
        1
        1  
        3               
        4
        4
        5
        7
        2
        6
        6   怎样写Sql语句能查出重复small_id值的数量, 也就是(有三个重复值,分别是1,4,6),查询结果为3(既重复值的个数)

解决方案 »

  1.   

    select count(1) from A group by small_id having count(small_id)>1
      

  2.   

    --try
    select count(distinct small_id) from tb group by small_id having count(*)>1 
      

  3.   

    SELECT COUNT(*) FROM
    (SELECT small_id  FROM TB T WHERE EXISTS(SELECT 1 FROM TB WHERE small_id   =T.small_id )AS T
      

  4.   

    select count(distinct small_id) 
    from tb 
    group by small_id 
    having count(*)>1 
      

  5.   

    select [small_id],count([small_id]) from TB group by [small_id] having count(1)>1
      

  6.   


    select count(*)as num from
    (select small_id from Tb group by small_id having(count(*)>1)
    )t
      

  7.   

    SELECT COUNT( DISTINCT small_id  ) 
    FROM
    (SELECT small_id  FROM TB T WHERE EXISTS(SELECT 1 FROM TB WHERE small_id   =T.small_id )AS T
      

  8.   

    --> Title:生成測試數據
    -->Author:wufeng4552【水族杰纶】
    -->Date :2009-08-19 09:42:23
     
    if not object_id('tb') is null
    drop table tb
    Go
    Create table tb([small_id] int)
    Insert tb
    select 1 union all
    select 1 union all
    select 3 union all
    select 4 union all
    select 4 union all
    select 5 union all
    select 7 union all
    select 2 union all
    select 6 union all
    select 6
    Go
    select count(small_id) from( 
    select  small_id from tb group by small_id having count(*)>1 
    )t
    /*
    -----------
    3(1 個資料列受到影響)
    */
      

  9.   

    select top 1 count(1) from A group by small_id having count(small_id)>1