请教一个SQL问题

gamehero 2003-10-19 06:06:02
表构如下:
id c1 c2

0 1 0
1 1 1
2 0 0
3 0 0
4 1 1

求C1=1的数量和C2=1的数量
能不能用一句SQL实现?
也就是3,2
...全文
34 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
gamehero 2003-10-27
  • 打赏
  • 举报
回复
谢谢各位!
结贴啦~~~~
lijinqiang 2003-10-19
  • 打赏
  • 举报
回复
SELECT COUNT(C1) FROM 表 where c1='1'
union all
SELECT COUNT(C1) FROM 表 where c2='1'
orcale 2003-10-19
  • 打赏
  • 举报
回复
declare @t table(id int,c1 int,c2 int)
insert @t select 0,1,0 union all
select 1,1,1 union all
select 2,0,0 union all
select 3,0,0 union all
select 4,1,1
select count(*) as total1 from @t where c1=1 union all
select count(*) as total2 from @t where c2=1
zarge 2003-10-19
  • 打赏
  • 举报
回复
select sum(case when c1 = 1 then 1 else 0 end) as c1的数量,
sum(case when c2 = 1 then 1 else 0 end) as c2的数量
from 表
welyngj 2003-10-19
  • 打赏
  • 举报
回复
select (select count(c1) c1 from ac where c1=1) sumc1 ,(select count(c2)
from ac where c2=1) sumc2

34,575

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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