如:
select count(*) as 申请补充人数 from person
select count(*) as 已经补充 from person where sfyjbc='已经补充'
select count(*) as 未补充 from person where sfyjbc=' '我用count计算出人以上三个字段的值。
最终想实现的事。select A..... from..结果:
字段1 申请补充人数 已经补充 未补充
A 20 18 2
现在不知道这个应该怎么连接起来。
谢谢大家了。
select count(*) as 申请补充人数 from person
select count(*) as 已经补充 from person where sfyjbc='已经补充'
select count(*) as 未补充 from person where sfyjbc=' '我用count计算出人以上三个字段的值。
最终想实现的事。select A..... from..结果:
字段1 申请补充人数 已经补充 未补充
A 20 18 2
现在不知道这个应该怎么连接起来。
谢谢大家了。
union
select 0 as 申请补充人数,count(*) as 已经补充,0 未补充 from person where sfyjbc='已经补充'
unioin
select 0 as 申请补充人数,0 as 已经补充,count(*) as 未补充 from person where sfyjbc=' '
select sum(申请补充人数) as 申请补充人数,sum(已经补充) as 已经补充,sum(未补充) as 未补充 from (上面那一组sql)
select * from
(select count(*) as 申请补充人数 from person ) t1,
(select count(*) as 已经补充 from person where sfyjbc='已经补充') t2,
(select count(*) as 未补充 from person where sfyjbc=' ') t3
(SELECT COUNT(*) FROM [person] WHERE [sfyjbc]='2234') AS [已经补充],
(SELECT COUNT(*) FROM [person] WHERE [sfyjbc]=' ') AS [未补充]
FROM [person]
字段1中还有数据。字段1 申请补充人数 已经补充 未补充
A 20 18 2
B 20 15 5
select count(*) as 已经补充 from person where sfyjbc='已经补充'
select count(*) as 未补充 from person where sfyjbc=' '后面根本没有字段1的限制 怎么会出现不同的数据的?
A 是 已经补充
A 否
A 是
A 是 已经补充
A 是 已经补充
B 是 已经补充
B 是
B 是 已经补充
B 是 已经补充
B 是 已经补充
大概是这样子的,如果需要补充就是申请补充人数,已经补充 空格 表示还没有补充的。申请补充减去已经补充的,就是未补充的。
现在我想统计A.B下面 各有 多少需要补充的,已经补充了多少,还有多少没有补充。
insert #
select 'A','已经补充' union all
select 'A','' union all
select 'A','' union all
select 'B','' union all
select 'B','已经补充'
select fd1,
sum(case sfyjbc when '已经补充' then 1 else 1 end) as [总数],
sum(case sfyjbc when '已经补充' then 1 else 0 end) as [已经补充],
sum(case sfyjbc when '' then 1 else 0 end) as [未补充]
from # group by fd1/*
fd1 总数 已经补充 未补充
A 3 1 2
B 2 1 1
*/
select 'A','是','已经补充' union all
select 'A','是','已经补充' union all
select 'A','否',''union all
select 'A','是',''union all
select 'A','是','已经补充'union all
select 'A','是','已经补充'union all
select 'B','是','已经补充'union all
select 'B','是',''union all
select 'B','是','已经补充'union all
select 'B','是','已经补充'union all
select 'B','是','已经补充'
select fd1,
sum(case flag when '是' then 1 else 0 end) as [需要补充数],
sum(case sfyjbc when '已经补充' then 1 else 1 end) as [总数],
sum(case sfyjbc when '已经补充' then 1 else 0 end) as [已经补充],
sum(case sfyjbc when '' then 1 else 0 end) as [未补充]
from # group by fd1/*
fd1 需要补充数 总数 已经补充 未补充
A 5 6 4 2
B 5 5 4 1
*/