表 A
ID,userid,group
1,    01      1001
2,    01      1001
3,    02      1001
4,    03      1002
5,    03      1001
6,    04      1002
7,    04      1002
8,    05      1002求 每个group 下的userid 的个数。大致的结果为
1001  3
1002  3

解决方案 »

  1.   

    select [group],count(1) num
    from tb
    group by [group]
      

  2.   

    select [group],count(distinct userid) num
    from tb
    group by [group]
      

  3.   

    select group, count(distinct userid) from tb group by group
      

  4.   


    select group, 
           count(distinct userid) 
    from [table] group by group
      

  5.   

    select group,count(distinct userid) from test1 group by group
      

  6.   

    Create table test1 
    (id int,
    userid VARCHAR(10),
    GROUPID VARCHAR(10)
    )Insert into test1
    select 1, '01', '1001' union all
    select 2, '01', '1001' union all
    select 3, '02', '1001' union all
    select 4, '03', '1002' union all
    select 5, '03', '1001' union all
    select 6, '04', '1002' union all
    select 7, '04', '1002' union all
    select 8, '05', '1002'
    select groupid,count(distinct userid) from test1 group by groupid