有个表格T1的结构是这样的:ID、NAME
T1数据:
ID   Name                
1     优
2     良
3     良
4     好
5     好
6     优
7     好
结果:优的总数     良的总数     好的总数
        2              2            3

解决方案 »

  1.   

    横起来不会,但可以竖起来:
    SELECT Name, Count(*)
    From T1
    GROUP Name
      

  2.   

    --建立测试环境
    Create Table 表(ID varchar(10),Name varchar(10))
    --插入数据
    insert into 表
    select '1','优' union
    select '2','良' union
    select '3','良' union
    select '4','好' union
    select '5','好' union
    select '6','优' union
    select '7','好'--测试语句
     select 优=sum(case when name='优' then 1 else 0 end), 
    良=sum(case when name='良' then 1 else 0 end), 
    好=sum(case when name='好' then 1 else 0 end) 
    from 表
     
    --删除测试环境
    Drop Table 表
      

  3.   

    SELECT Name, Count(*)
    From T1
    GROUP BY Name
      

  4.   

    Select Count(*) AS 优的总数  From T1 Where Name = '优'
    Select Count(*) AS 良的总数  From T1 Where Name = '良'
    Select Count(*) AS 好的总数  From T1 Where Name = '好'
      

  5.   

    同意  Demogodyou(大漠孤雕)的回复
      

  6.   

    select 
      (Select Count(*) AS 优的总数  From T1 Where Name = '优') as '优的总数',
      (Select Count(*) AS 良的总数  From T1 Where Name = '良') as '良的总数'
      (Select Count(*) AS 好的总数  From T1 Where Name = '好') as '好的总数'
    into 
      totleresult
    用完后删除table  totleresult
      

  7.   

    试试这样写行不?
    select count(name) 优的总数 from T1 where Name ='优' UNION select count(name) 良的总数from T1 where Name ='良' UNION select count(name) 好的总数 from T1 where Name ='好'
      

  8.   

    select 优=sum(case when name='优' then 1 else 0 end), 
    良=sum(case when name='良' then 1 else 0 end), 
    好=sum(case when name='好' then 1 else 0 end) 
    from 表