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

解决方案 »

  1.   

    结果为
    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%
      

  2.   

    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))+'%'
    from [tb] t
    group by qy,team
      

  3.   

    --> 测试数据:[tb]
    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%
      

  4.   

    --这个指标看不明白.quyuRate
    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 行)
    */
      

  5.   

    好象看明白了.
    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
      

  6.   

    其实是先得到这样的结果:qy team shenAll shen1
    小明 大明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