PKID  number  Disc  title  superior Itemlevel state
1      5.1     AA    1       0         1       0
2      5.1     BB    1.1     1         0       2
3      5.1     CC    1.2     1         0       0
4      5.1     DD    2       0         1       0
5      5.1     EE    2.1     4         0       2
6      5.1     FF    2.2     4         0       2
7      5.1     HH    2.3     4         0       0
8      5.1     II    3       0         1       2
9      5.1     JJ    4       0         0       0注明:这是个项目树,superior表示上下组与PKID有关系.superior=0表示根节点,节点个数看这个..Itemleavel的1表示已分解,0表示未分解除,state的0表示未完成,2表示完成.结果如下:
pkid   number  disc  title 总节点数 未完成  已完成 完成进度
1      5.1     AA     1     2        1       1       50%
4      5.1     DD     2     3        1       2       66.66%
8      5.1     II     3     0        0       1       100%
9      5.1     JJ     4     0        1       0       0%问这个结果是怎么得出来的?

解决方案 »

  1.   

    这个问题不是有人提了吗? 咳没法子再给他发一次把
    create table aaa(PKID int,  number decimal(18,1) , 
    Disc varchar(10), title decimal(18,1), 
    superior int, Itemlevel int, state int)
    insert into aaa
    select 1   ,   5.1 ,    'AA',    1    ,   0   ,      1  ,     0
    union all select 2   ,   5.1 ,    'BB',    1.1  ,   1   ,      0  ,     2
    union all select 3   ,   5.1 ,    'CC',    1.2  ,   1   ,      0  ,     0
    union all select 4   ,   5.1 ,    'DD',    2    ,   0   ,      1  ,     0
    union all select 5   ,   5.1 ,    'EE',    2.1  ,   4   ,      0  ,     2
    union all select 6   ,   5.1 ,    'FF',    2.2  ,   4    ,     0  ,     2
    union all select 7   ,   5.1 ,    'HH',    2.3  ,   4     ,    0  ,     0
    union all select 8   ,   5.1 ,    'II',    3    ,   0     ,    1  ,     2
    union all select 9   ,   5.1 ,    'JJ',   4   ,    0    ,     0  ,     0select * from aaaselect a.PKID, a.number,
    a.disc ,a.title,
    (select count (b.title) from
     (select * from aaa where superior<> 0  ) b
     where a.title<b.title and b.title<a.title+1) as 总节点数,
    ---总节点数
    (select count (b.title) from
     aaa b
     where a.title<=b.title and b.title<a.title+1 
    and state=0 and b.Itemlevel=0) as 未完成,
    (select count (b.title) from
      aaa  b
     where a.title<=b.title and b.title<a.title+1 and state=2 ) as 已完成,
    ---已完成
    (case when ((select count (b.title) from
      aaa  b
     where a.title<=b.title and b.title<a.title+1 and state=2 )+
    (select count (b.title) from
     aaa b
     where a.title<=b.title and b.title<a.title+1 
    and state=0 and b.Itemlevel=0) )=0
    then 0 else 
    cast((select count (b.title) from
     aaa b
     where a.title<=b.title and b.title<a.title+1 
    and state=2) as float)/((select count (b.title) from
      aaa  b
     where a.title<=b.title and b.title<a.title+1 and state=2 )+
    (select count (b.title) from
     aaa b
     where a.title<=b.title and b.title<a.title+1 
    and state=0 and b.Itemlevel=0) ) end) as 完成进度
    -------完成进度
    from aaa a where 
    a.superior=0
      

  2.   

    子查询的最好测试题目。
    第一步做到统计出总节点个数
    第二步做到统计出总节点个数,已完成节点个数(state=2)
    第三步做到统计出总节点个数,已完成节点个数,未完成节点个数(总数-已完成)
    第四步做到统计出总节点个数,已完成节点个数,未完成节点个数,已完成百分比(已完成/总数)从简单,到容易,一步一步就做出来了。