求SQL统计不同的发贴状态

huangchao 2005-05-28 03:18:39
求SQL统计不同的发贴状态
===============================
现有表[Question],字段[Status]分别有值1,2,3,4,如何能一次统计出各种状态有多少条,现在我只会一个一个的统计,就是写四句:
select count(*) as cs1 from [Question] where [Status] = 1;
select count(*) as cs2 from [Question] where [Status] = 2;
select count(*) as cs3 from [Question] where [Status] = 3;
select count(*) as cs4 from [Question] where [Status] = 4;

请高手赐教!谢!

...全文
36 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
huangchao 2005-05-28
  • 打赏
  • 举报
回复
谢谢两位,还有一个问题
http://community.csdn.net/Expert/topic/4042/4042313.xml?temp=.6730158

我先结贴了,看到的话帮个忙!
paoluo 2005-05-28
  • 打赏
  • 举报
回复
呵,楼主已经发现了,另外一种方法也可以的。
paoluo 2005-05-28
  • 打赏
  • 举报
回复
或者


select cs1=Count(case when [Status] = 1 then 1 else Null end),
cs2=Count(case when [Status] = 2 then 1 else Null end),
cs3=Count(case when [Status] = 3 then 1 else Null end),
cs4=Count(case when [Status] = 4 then 1 else Null end)
from [Question]
paoluo 2005-05-28
  • 打赏
  • 举报
回复
老大的多了个逗号。


select cs1=sum(case when [Status] = 1 then 1 else 0 end),
cs2=sum(case when [Status] = 2 then 1 else 0 end),
cs3=sum(case when [Status] = 3 then 1 else 0 end),
cs4=sum(case when [Status] = 4 then 1 else 0 end)
from [Question]
huangchao 2005-05-28
  • 打赏
  • 举报
回复
邹建,如果csdn没有了你,那将会是何等的状况呀,我都不敢想象了,牛人,再次谢谢你!

不过最后一个逗号不要,希望其他朋友注意,别拷贝错了,是
select cs1=sum(case when [Status] = 1 then 1 else 0 end),
cs2=sum(case when [Status] = 2 then 1 else 0 end),
cs3=sum(case when [Status] = 3 then 1 else 0 end),
cs4=sum(case when [Status] = 4 then 1 else 0 end)
from [Question]
zjcxc 2005-05-28
  • 打赏
  • 举报
回复
select cs1=sum(case when [Status] = 1 then 1 else 0 end),
cs2=sum(case when [Status] = 2 then 1 else 0 end),
cs3=sum(case when [Status] = 3 then 1 else 0 end),
cs4=sum(case when [Status] = 4 then 1 else 0 end),
from [Question]

27,579

社区成员

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

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