横起来不会,但可以竖起来: SELECT Name, Count(*) From T1 GROUP Name
--建立测试环境 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 表
SELECT Name, Count(*) From T1 GROUP BY Name
Select Count(*) AS 优的总数 From T1 Where Name = '优' Select Count(*) AS 良的总数 From T1 Where Name = '良' Select Count(*) AS 好的总数 From T1 Where Name = '好'
同意 Demogodyou(大漠孤雕)的回复
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
试试这样写行不? select count(name) 优的总数 from T1 where Name ='优' UNION select count(name) 良的总数from T1 where Name ='良' UNION select count(name) 好的总数 from T1 where Name ='好'
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 表
SELECT Name, Count(*)
From T1
GROUP Name
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 表
From T1
GROUP BY Name
Select Count(*) AS 良的总数 From T1 Where Name = '良'
Select Count(*) AS 好的总数 From T1 Where Name = '好'
(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
select count(name) 优的总数 from T1 where Name ='优' UNION select count(name) 良的总数from T1 where Name ='良' UNION select count(name) 好的总数 from T1 where Name ='好'
良=sum(case when name='良' then 1 else 0 end),
好=sum(case when name='好' then 1 else 0 end)
from 表