有十条记录
编号 需要完成数量 已经完成数量 总数量
1 20 10 100
2 20 15 100
3 20 30 100
4 20 35 100
5 20 10 100
6 20 20 100
7 20 20 100
8 20 18 100
9 20 22 100
10 20 20 100需要统计的是:select count(已完成的数量=需要完成的数量) as 正常完成,count(已完成的数量<需要完成的数量) as 未完成,count(已完成的数量>需要完成的数量) as 超额完成,cast(count(刚好达到+超额部分/总数量) as decimal(18,4))*100 as 完成比率,cast(count(超额/总数量) as decimal(18,4)*100 as 超额比率 from table--------------------------------------------------------------------------------------
正常完成 未完成 超额完成 完成比率 超额比率
--------------------------------------------------------------------------------------
3 4 3 6% 3%还请各位帮忙~~~。分数不够再加~~
编号 需要完成数量 已经完成数量 总数量
1 20 10 100
2 20 15 100
3 20 30 100
4 20 35 100
5 20 10 100
6 20 20 100
7 20 20 100
8 20 18 100
9 20 22 100
10 20 20 100需要统计的是:select count(已完成的数量=需要完成的数量) as 正常完成,count(已完成的数量<需要完成的数量) as 未完成,count(已完成的数量>需要完成的数量) as 超额完成,cast(count(刚好达到+超额部分/总数量) as decimal(18,4))*100 as 完成比率,cast(count(超额/总数量) as decimal(18,4)*100 as 超额比率 from table--------------------------------------------------------------------------------------
正常完成 未完成 超额完成 完成比率 超额比率
--------------------------------------------------------------------------------------
3 4 3 6% 3%还请各位帮忙~~~。分数不够再加~~
count(case when 已完成的数量=需要完成的数量 then 1 else 0 end) as 正常完成,
count(case when 已完成的数量<需要完成的数量 then 1 else 0 end) as 未完成,
count(case when 已完成的数量>需要完成的数量 then 1 else 0 end) as 超额完成,
cast(count(刚好达到+超额部分/总数量) as decimal(18,4))*100 as 完成比率, --这不太明白怎么意思
cast(count(超额/总数量) as decimal(18,4)*100 as 超额比率 --这不太明白怎么意思
from table
count(case when 已完成的数量=需要完成的数量 then 1 else 0 end) as 正常完成,
count(case when 已完成的数量<需要完成的数量 then 1 else 0 end) as 未完成,
count(case when 已完成的数量>需要完成的数量 then 1 else 0 end) as 超额完成,
cast(count(刚好达到+超额部分/总数量) as decimal(18,4))*100 as 完成比率, --这不太明白怎么意思
cast(count(超额/总数量) as decimal(18,4)*100 as 超额比率 --这不太明白怎么意思
from table
-------------------------------------------> 测试时间:2009-07-16
--> 我的淘宝:http://shop36766744.taobao.com/--------------------------------------------------if object_id('[TB]') is not null drop table [TB]
create table [TB]([编号] int,[需要完成数量] int,[已经完成数量] int,[总数量] int)
insert [TB]
select 1,20,10,100 union all
select 2,20,15,100 union all
select 3,20,30,100 union all
select 4,20,35,100 union all
select 5,20,10,100 union all
select 6,20,20,100 union all
select 7,20,20,100 union all
select 8,20,18,100 union all
select 9,20,22,100 union all
select 10,20,20,100select * from [TB]select 正常完成=sum(case when 已经完成数量=需要完成数量 then 1 else 0 end),
未完成=sum(case when 已经完成数量 <需要完成数量 then 1 else 0 end),
超额完成=sum(case when 已经完成数量>需要完成数量 then 1 else 0 end),
完成比率=cast(sum((case when 已经完成数量>=需要完成数量 then 1 else 0 end))*100/count(*) as varchar(4))+'%',
超额比率=cast(sum(case when 已经完成数量>需要完成数量 then 1 else 0 end)*100/count(*) as varchar(4))+'%'
from TB/*
正常完成 未完成 超额完成 完成比率 超额比率
----------- ----------- ----------- ----- -----
3 4 3 60% 30%(所影响的行数为 1 行)*/drop table TB
insert into @t select 1 ,20,10,100
insert into @t select 2 ,20,15,100
insert into @t select 3 ,20,30,100
insert into @t select 4 ,20,35,100
insert into @t select 5 ,20,10,100
insert into @t select 6 ,20,20,100
insert into @t select 7 ,20,20,100
insert into @t select 8 ,20,18,100
insert into @t select 9 ,20,22,100
insert into @t select 10,20,20,100
select
sum(case when 需要完成数量=已经完成数量 then 1 else 0 end) as 正常完成,
sum(case when 需要完成数量>已经完成数量 then 1 else 0 end) as 未完成 ,
sum(case when 需要完成数量<已经完成数量 then 1 else 0 end) as 超额完成,
rtrim(sum(case when 需要完成数量>已经完成数量 then 0 else 已经完成数量*100/总数量 end))+'%' as 完成比率,
rtrim(sum(case when 需要完成数量<已经完成数量 then (已经完成数量-需要完成数量)*100/总数量 else 0 end))+'%' as 超额比率
from
@t/*
正常完成 未完成 超额完成 完成比率 超额比率
----------- ----------- ----------- ------------- -------------
3 4 3 147% 27%
*/
select sum(case when 已经完成数量=需要完成数量 then 1 else 0 end) as 正常完成,
sum(case when 已经完成数量 <需要完成数量 then 1 else 0 end) as 未完成,
sum(case when 已经完成数量>需要完成数量 then 1 else 0 end) as 超额完成,
完成比率=cast(sum((case when 已经完成数量>=需要完成数量 then 1 else 0 end))*100/count(*) as varchar(4))+'%',
超额比率=cast(sum(case when 已经完成数量>需要完成数量 then 1 else 0 end)*100/count(*) as varchar(4))+'%'
from [table]
drop table [TB]
go
create table [TB]([编号] int,[需要完成的数量] int,[已完成的数量] int,[数量] int)
insert [TB]
select 1,20,10,100 union all
select 2,20,15,100 union all
select 3,20,30,100 union all
select 4,20,35,100 union all
select 5,20,10,100 union all
select 6,20,20,100 union all
select 7,20,20,100 union all
select 8,20,18,100 union all
select 9,20,22,100 union all
select 10,20,20,100
go
select
sum (case when 已完成的数量=需要完成的数量 then 1 else 0 end) as 正常完成,
sum(case when 已完成的数量<需要完成的数量 then 1 else 0 end) as 未完成,
sum(case when 已完成的数量>需要完成的数量 then 1 else 0 end) as 超额完成,
convert(varchar,cast(((sum (case when 已完成的数量=需要完成的数量 then 1 else 0 end) + sum(case when 已完成的数量>需要完成的数量 then 1 else 0 end))*1.0/sum(数量))*100 as decimal(18,2))) +'%' as 完成比率,
convert(varchar,cast ((sum(case when 已完成的数量>需要完成的数量 then 1 else 0 end)*1.0/sum(数量))*100 as decimal(18,2)))+'%'as 超额比率
from tb(10 行受影响)
正常完成 未完成 超额完成 完成比率 超额比率
----------- ----------- ----------- ------------------------------- -------------------------------
3 4 3 0.60% 0.30%(1 行受影响)
drop table [TB]
go
create table [TB]([编号] int,[需要完成的数量] int,[已完成的数量] int,[数量] int)
insert [TB]
select 1,20,10,100 union all
select 2,20,15,100 union all
select 3,20,30,100 union all
select 4,20,35,100 union all
select 5,20,10,100 union all
select 6,20,20,100 union all
select 7,20,20,100 union all
select 8,20,18,100 union all
select 9,20,22,100 union all
select 10,20,20,100
go
select
sum (case when 已完成的数量=需要完成的数量 then 1 else 0 end) as 正常完成,
sum(case when 已完成的数量<需要完成的数量 then 1 else 0 end) as 未完成,
sum(case when 已完成的数量>需要完成的数量 then 1 else 0 end) as 超额完成,
convert(varchar,cast(((sum (case when 已完成的数量=需要完成的数量 then 1 else 0 end) + sum(case when 已完成的数量>需要完成的数量 then 1 else 0 end))*1.0/sum(数量))*100as decimal(18,2))) as 完成比率,
convert(varchar,cast ((sum(case when 已完成的数量>需要完成的数量 then 1 else 0 end)*1.0/sum(数量))*100 as decimal(18,2)))as 超额比率
from tb(10 行受影响)
正常完成 未完成 超额完成 完成比率 超额比率
----------- ----------- ----------- ------------------------------ ------------------------------
3 4 3 0.60 0.30(1 行受影响)
drop table [TB]
go
create table [TB]([编号] int,[需要完成的数量] int,[已完成的数量] int,[数量] int)
insert [TB]
select 1,20,10,100 union all
select 2,20,15,100 union all
select 3,20,30,100 union all
select 4,20,35,100 union all
select 5,20,10,100 union all
select 6,20,20,100 union all
select 7,20,20,100 union all
select 8,20,18,100 union all
select 9,20,22,100 union all
select 10,20,20,100
go
select
sum (case when 已完成的数量=需要完成的数量 then 1 else 0 end) as 正常完成,
sum(case when 已完成的数量<需要完成的数量 then 1 else 0 end) as 未完成,
sum(case when 已完成的数量>需要完成的数量 then 1 else 0 end) as 超额完成,
convert(varchar,cast(((sum (case when 已完成的数量=需要完成的数量 then [已完成的数量] else 0 end) + sum(case when 已完成的数量>需要完成的数量 then 1 else 0 end))*1.0/sum(数量))*100as decimal(18,2))) +'%' as 完成比率,
convert(varchar,cast ((sum(case when 已完成的数量>需要完成的数量 then ([已完成的数量]-[需要完成的数量]) else 0 end)*1.0/sum(数量))*100 as decimal(18,2)))+'%'as 超额比率
from tb
/*
正常完成 未完成 超额完成 完成比率 超额比率
----------- ----------- ----------- ------------------------------- -------------------------------
3 4 3 6.30% 2.70%(1 行受影响)
最终正确答案
drop table [TB]
go
create table [TB]([编号] int,[需要完成的数量] int,[已完成的数量] int,[数量] int)
insert [TB]
select 1,20,10,100 union all
select 2,20,15,100 union all
select 3,20,30,100 union all
select 4,20,35,100 union all
select 5,20,10,100 union all
select 6,20,20,100 union all
select 7,20,20,100 union all
select 8,20,18,100 union all
select 9,20,22,100 union all
select 10,20,20,100
go
select
sum (case when 已完成的数量=需要完成的数量 then 1 else 0 end) as 正常完成,
sum(case when 已完成的数量<需要完成的数量 then 1 else 0 end) as 未完成,
sum(case when 已完成的数量>需要完成的数量 then 1 else 0 end) as 超额完成,
convert(varchar,cast(((sum (case when 已完成的数量=需要完成的数量 then [已完成的数量] else 0 end) + sum(case when 已完成的数量>需要完成的数量 then 1 else 0 end))*1.0/sum(数量))*100as decimal(18,0))) +'%' as 完成比率,
convert(varchar,cast ((sum(case when 已完成的数量>需要完成的数量 then ([已完成的数量]-[需要完成的数量]) else 0 end)*1.0/sum(数量))*100 as decimal(18,0)))+'%'as 超额比率
from tb
/*
正常完成 未完成 超额完成 完成比率 超额比率
----------- ----------- ----------- ------------------------------- -------------------------------
3 4 3 6% 3%(1 行受影响)
完美终极答案
超额比率=超额完成 / 总数量 * 100%结合上面的方法
sum(case when 已经完成数量=需要完成数量 then 1 else 0 end) as 正常完成,
sum(case when 已经完成数量>需要完成数量 then 1 else 0 end) as 超额完成总数量=100
convert(varchar,cast(((sum (case when 已完成的数量=需要完成的数量 then [已完成的数量] else 0 end) + sum(case when 已完成的数量>需要完成的数量 then 1 else 0 end))*1.0/sum(数量))*100as decimal(18,0))) +'%' as 完成比率,
convert(varchar,cast ((sum(case when 已完成的数量>需要完成的数量 then ([已完成的数量]-[需要完成的数量]) else 0 end)*1.0/sum(数量))*100 as decimal(18,0)))+'%'as 超额比率 如果sum(数量) 是=count(distinct QTY) 这要怎么改??
select
sum (case when 已完成的数量=需要完成的数量 then 1 else 0 end) as 正常完成,
sum(case when 已完成的数量<需要完成的数量 then 1 else 0 end) as 未完成,
sum(case when 已完成的数量>需要完成的数量 then 1 else 0 end) as 超额完成,
convert(varchar,cast(((sum (case when 已完成的数量=需要完成的数量 then 1 else 0 end) + sum(case when 已完成的数量>需要完成的数量 then 1 else 0 end))*1.0/sum(数量))*100as decimal(18,2))) as 完成比率,
convert(varchar,cast ((sum(case when 已完成的数量>需要完成的数量 then 1 else 0 end)*1.0/sum(distinct 数量))*100 as decimal(18,2)))as 超额比率
from tb