shen qy team
1 小明 大明1
1 小明 大明1
0 小明 大明2
1 小明 大明2
1 小明 大明1
1 小明 大明4
1 小明 大明3
0 小蒋 大江4
1 小蒋 大江1
1 小蒋 大江1
0 小蒋 大江4
1 小毛 大毛2
0 小毛 大毛1
1 小毛 大毛1
1 小毛 大毛1
0 小李 大力水手4
1 小李 大力水手4
1 小李 大力水手3
0 小李 大力水手3要求统计结果
[code]
qy team shenAll shen1 qyRate teamRate
小明 大明1 3 3 57.14% 100.00%
小明 大明3 1 1 57.14% 100.00%
小明 大明2 2 1 57.14% 50.00%
小明 大明4 1 0 57.14% 0.00%
小蒋 大江4 2 0 50.00% 0.00%
小蒋 大江1 2 2 50.00% 100.00%
小毛 大毛1 3 2 75.00% 67.00%
小毛 大毛2 1 1 75.00% 100.00%
小李 大力3 2 1 50.00% 50.00%
小李 大力4 2 1 50.00% 50.00%
[/code]
统计规则:根据team分组,shenAll为shen的计数,shen1为对shen=1的计数,
qyRate是按照qy分组sum(shen1)/sum(shenAll),teamRate是shen1/shenAll
解决方案 »
- 存储过程调用,格式转换问题
- 用08的数据库打不开05的,提示好像“执行transact-SQL语句或批处理时发生异常”,接着后面是一连串的版本升级显示
- sqlserver2005的触发器怎么样能逐行执行
- 装了sql server2000后,再装service pack4,sql server不能用?
- 2000-->7.0 求解密高手!
- 建立数据库的问题?
- 救命啊,我给最高分!
- 请问各位大虾,用DAO如何连接到SQLServer,拜托急急急急急急!!!!!!!
- 请教各位高手:MS-SQL Server对每一个进程的连接数是不是做了限制
- 简单的题目,来指点一下菜鸟
- 执行DBCC REBUILD_LOG,提示此数据库的日志文件已处于活动状态。
- 请教一个SQL代码
qy team shenAll shen1 quyuRate teamRate
小明 大明1 3 3 57.14% 100.00%
小明 大明3 1 1 57.14% 100.00%
小明 大明2 2 1 57.14% 50.00%
小明 大明4 1 0 57.14% 0.00%
小蒋 大江4 2 0 50.00% 0.00%
小蒋 大江1 2 2 50.00% 100.00%
小毛 大毛1 3 2 75.00% 67.00%
小毛 大毛2 1 1 75.00% 100.00%
小李 大力3 2 1 50.00% 50.00%
小李 大力4 2 1 50.00% 50.00%
ltrim(cast((select sum(shen) from [tb] where qy=t.qy)*100.0/(select count(1) from [tb] where qy=t.qy) as dec))+'%' as quyuRate,
ltrim(cast(sum(shen)*100.0/count(1) as dec))+'%'
from [tb] t
group by qy,team
if object_id('[tb]') is not null drop table [tb]
create table [tb]([shen] int,[qy] varchar(4),[team] varchar(9))
insert [tb]
select 1,'小明','大明1' union all
select 1,'小明','大明1' union all
select 0,'小明','大明2' union all
select 1,'小明','大明2' union all
select 1,'小明','大明1' union all
select 1,'小明','大明4' union all
select 1,'小明','大明3' union all
select 0,'小蒋','大江4' union all
select 1,'小蒋','大江1' union all
select 1,'小蒋','大江1' union all
select 0,'小蒋','大江4' union all
select 1,'小毛','大毛2' union all
select 0,'小毛','大毛1' union all
select 1,'小毛','大毛1' union all
select 1,'小毛','大毛1' union all
select 0,'小李','大力水手4' union all
select 1,'小李','大力水手4' union all
select 1,'小李','大力水手3' union all
select 0,'小李','大力水手3'
select qy,team,count(1) as shenAll,sum(shen) as shen1,
ltrim(cast((select sum(shen) from [tb] where qy=t.qy)*100.0/(select count(1) from [tb] where qy=t.qy) as dec))+'%' as quyuRate,
ltrim(cast(sum(shen)*100.0/count(1) as dec))+'%' as teamRate
from [tb] t
group by qy,team
order by qy desc
----------------------------------
小明 大明1 3 3 86% 100%
小明 大明2 2 1 86% 50%
小明 大明3 1 1 86% 100%
小明 大明4 1 1 86% 100%
小毛 大毛1 3 2 75% 67%
小毛 大毛2 1 1 75% 100%
小李 大力水手3 2 1 50% 50%
小李 大力水手4 2 1 50% 50%
小蒋 大江1 2 2 50% 100%
小蒋 大江4 2 0 50% 0%
select qy,
team ,
shenAll = count(1),
shen1 = (select count(1) from tb where shen = 1 and qy = t.qy and team = t.team),
teamRate = cast((select count(1) from tb where shen = 1 and qy = t.qy and team = t.team)*100.0/count(1) as decimal(18,2))
from tb t
group by qy , team/*
qy team shenAll shen1 teamRate
---------- ---------- ----------- ----------- --------------------
小蒋 大江1 2 2 100.00
小蒋 大江4 2 0 .00
小李 大力水手3 2 1 50.00
小李 大力水手4 2 1 50.00
小毛 大毛1 3 2 66.67
小毛 大毛2 1 1 100.00
小明 大明1 3 3 100.00
小明 大明2 2 1 50.00
小明 大明3 1 1 100.00
小明 大明4 1 1 100.00(所影响的行数为 10 行)
*/
create table tb(shen int, qy varchar(10), team varchar(10))
insert into tb values(1 , '小明' , '大明1')
insert into tb values(1 , '小明' , '大明1')
insert into tb values(0 , '小明' , '大明2')
insert into tb values(1 , '小明' , '大明2')
insert into tb values(1 , '小明' , '大明1')
insert into tb values(1 , '小明' , '大明4')
insert into tb values(1 , '小明' , '大明3')
insert into tb values(0 , '小蒋' , '大江4')
insert into tb values(1 , '小蒋' , '大江1')
insert into tb values(1 , '小蒋' , '大江1')
insert into tb values(0 , '小蒋' , '大江4')
insert into tb values(1 , '小毛' , '大毛2')
insert into tb values(0 , '小毛' , '大毛1')
insert into tb values(1 , '小毛' , '大毛1')
insert into tb values(1 , '小毛' , '大毛1')
insert into tb values(0 , '小李' , '大力水手4')
insert into tb values(1 , '小李' , '大力水手4')
insert into tb values(1 , '小李' , '大力水手3')
insert into tb values(0 , '小李' , '大力水手3')
goselect qy,
team ,
shenAll = count(1),
shen1 = (select count(1) from tb where shen = 1 and qy = t.qy and team = t.team),
quyuRate = cast((select count(1) from tb where shen = 1 and qy = t.qy )*100.0/(select count(1) from tb where qy = t.qy) as decimal(18,2)),
teamRate = cast((select count(1) from tb where shen = 1 and qy = t.qy and team = t.team)*100.0/count(1) as decimal(18,2))
from tb t
group by qy , team/*
qy team shenAll shen1 quyuRate teamRate
---------- ---------- ----------- ----------- -------------------- --------------------
小蒋 大江1 2 2 50.00 100.00
小蒋 大江4 2 0 50.00 .00
小李 大力水手3 2 1 50.00 50.00
小李 大力水手4 2 1 50.00 50.00
小毛 大毛1 3 2 75.00 66.67
小毛 大毛2 1 1 75.00 100.00
小明 大明1 3 3 85.71 100.00
小明 大明2 2 1 85.71 50.00
小明 大明3 1 1 85.71 100.00
小明 大明4 1 1 85.71 100.00(所影响的行数为 10 行)
*/drop table tb
小明 大明1 3 3
小明 大明3 1 1
小明 大明2 2 1
小明 大明4 1 0
小蒋 大江4 2 0
小蒋 大江1 2 2
小毛 大毛1 3 2
小毛 大毛2 1 1
小李 大力3 2 1
小李 大力4 2 1然后在这个结果的基础上再统计:
quyurate是sum(shen1)/sum(allshen),比如小明的:(3+1+1+0)/(3+1+2+1)
teamrate是shen1/allshen,比如小明的大明1:3/3;小明的大明2:1/2