select 列1,count(列1) from table1 group by 列1

解决方案 »

  1.   

    select col1 , count(distinct col2) from tb group by col1
      

  2.   


    /*过滤重复的直接加distinct*/select distinct 列1,列2 from table1
      

  3.   

    create table TB(col1 varchar(10),    col2 varchar(10))
    insert into tb values('1111' ,   'a') 
    insert into tb values('1111' ,   'b') 
    insert into tb values('1111' ,   'b') 
    insert into tb values('1111' ,   'c') 
    insert into tb values('2222' ,   'x') 
    insert into tb values('2222' ,   'y') 
    insert into tb values('2222' ,   'y') 
    insert into tb values('2222' ,   'z') 
    goselect col1 , count(distinct col2) from tb group by col1drop table tb/*
    col1                   
    ---------- ----------- 
    1111       3
    2222       3(所影响的行数为 2 行)*/
      

  4.   

    create table TB(col1 varchar(10),    col2 varchar(10))
    insert into tb values('1111' ,   'a') 
    insert into tb values('1111' ,   'b') 
    insert into tb values('1111' ,   'b') 
    insert into tb values('1111' ,   'c') 
    insert into tb values('2222' ,   'x') 
    insert into tb values('2222' ,   'y') 
    insert into tb values('2222' ,   'y') 
    insert into tb values('2222' ,   'z') 
    goselect col1 , col2 = count(distinct col2) from tb group by col1drop table tb/*
    col1       col2             
    ---------- ----------- 
    1111       3
    2222       3(所影响的行数为 2 行)*/
      

  5.   


    /*要得到你那结果还是这样写吧*/select 列1,count(列2) from (
    select distinct 列1,列2 from table1 )x
      

  6.   

    create table #T
    (
     id nvarchar(20),
     letter nvarchar(20)
    )
    insert into #T
    select 
    '1111',    'a' union all 
    select 
    '1111',    'b' union all
    select 
    '1111',    'b' union all 
    select 
    '1111',    'c' union all 
    select  
    '2222',    'x' union all 
    select 
    '2222',    'y' union all 
    select 
    '2222',    'y' union all 
    select 
    '2222',    'z' 
    select id,COUNT(1) from
    (select distinct id,letter from #T) A
    group by id