group by 的大问题:有些类别没有显示出来

woft 2005-06-15 10:09:22
比如我有一个表a
id name desription score
1 zhang monitor 60
2 Li student 70
3 wang student 80

然后我写:select count(*) from a where score>65 group by description

结果只显示:2,student
而没有desription = monitor的信息。

但我的要求是显示
1, monitor
2,student

这个能做到吗?
...全文
213 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
bflovesnow 2005-06-15
  • 打赏
  • 举报
回复
hehe,正解很多
hsj20041004 2005-06-15
  • 打赏
  • 举报
回复
create table tb(id int,name varchar(20),desription varchar(20),score int)
insert into tb
select 1,'zhang','monitor',60
union select 2,'Li','student',70
union select 3,'wang','student',80
-----查询
select count(*) as 数量,desription from tb where score>65 group by desription
union select case when score>65 then 1 else 0 end as 数量,desription from tb where desription ='monitor'
----结果
数量 desription
----------- --------------------
0 monitor
2 student

(所影响的行数为 2 行)
woft 2005-06-15
  • 打赏
  • 举报
回复
强,接铁!
woft 2005-06-15
  • 打赏
  • 举报
回复
to spring_504(土人我不太明白这条sql的意思,不过
select distinct t.desription,isnull(SELECT COUNT(*) FROM a where desription=t.desription,0) FROM a t这条语句不能执行
服务器: 消息 156,级别 15,状态 1,行 1
在关键字 'SELECT' 附近有语法错误。
服务器: 消息 170,级别 15,状态 1,行 1
第 1 行: ',' 附近有语法错误。
LANBAgaga 2005-06-15
  • 打赏
  • 举报
回复
nub desription
0 monitor
2 student

(所影响的行数为 2 行)
LANBAgaga 2005-06-15
  • 打赏
  • 举报
回复
写错了,我的要求是显示
0, monitor
2,student


如下:

--create test table
create table a (desription varchar(100),score int)
insert into a select 'monitor', 60
insert into a select 'student', 70
insert into a select 'student', 80
--T-sql
select
sum(case when score>65 then 1 else 0 end)as nub,desription
from a group by desription
--drop table
drop table a
woft 2005-06-15
  • 打赏
  • 举报
回复
to LANBAgaga
你这样也只能显示student,不能显示monitor 因为monitor的score<65
spring_504 2005-06-15
  • 打赏
  • 举报
回复
select distinct t.desription ,isnull(SELECT COUNT(*) FROM a where esription=t.desription,0) FROM 表a t
LANBAgaga 2005-06-15
  • 打赏
  • 举报
回复
看错了

select count(*) from a group by description

你限定了score>65 ,当然不会有monitor了
woft 2005-06-15
  • 打赏
  • 举报
回复
写错了,我的要求是显示
0, monitor
2,student
LANBAgaga 2005-06-15
  • 打赏
  • 举报
回复
select count(*)as nub,desription from a where score>65 group by description

27,580

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧