我这个标题可能有点拗口哈,解释一下先。(1)什么是层次化百分比?
举个例子吧,比如:所购买的商品,大类里分中类,中类里分小类,小类里又分小小类,如此不停地细分下去,最后最底层是具体的商品和购买金额。用户问
      a)共花了多少钱,各大类各花了多少,占总开销的百分比是多少?
      b)每个大类里各中类又各花了多少,占该大类总开销的百分比是多少?
      c)每个中类里各小类又各花了多少,占该中类总开销的百分比是多少?
      d)每个小类里各小小类又各花了多少,占该小类总开销的百分比是多少?
      ……
这些问题可以一直问下去,对每个层次的节点都希望知道它占对应的上一层次的父节点的总和的百分比。这就是我说的层次化百分比的含义。(2)有关层次化数据
所以,这里面要进行百分比计算的基础数据是层次化的。可以形象地把它们想象成一棵树,父节点是上一层的类别,子节点是下一层的类别。现在要求出每个节点占自己的父节点的百分比。注意,这里除了最底层的叶子节点,其它节点实际上是没有自己的金额数据的,它的金额数据实际上是所有属于它这个类别的商品(也就是叶子节点)的金额的总计。(3)我的问题
为了求这样的层次化百分比,我该怎么安排表结构比较合理呢?具体地说,我该怎样设计节点表,节点表应该包含哪些字段呢?

解决方案 »

  1.   

    参照:http://expert.csdn.net/Expert/topic/866/866042.xml?temp=.1478998
      

  2.   


    --上面的贴子直接运行有点问题,整理了一下:drop table test_tree;
    create  table   test_tree   
      (   
          userid number(8) not null primary key,   
          username varchar2(18) not null,   
          p_username varchar2(18),   
          num number(10,2)   
      );   
        
      insert   into   test_tree   values(1,'1000',null,null);   
      insert   into   test_tree   values(2,'1100','1000',null);   
      insert   into   test_tree   values(3,'1200','1000',null);   
      insert   into   test_tree   values(4,'1101','1100',100);   
      insert   into   test_tree   values(5,'1102','1100',200);   
      insert   into   test_tree   values(6,'1104','1100',250);   
      insert   into   test_tree   values(7,'1201','1200',150);   
      insert   into   test_tree   values(8,'1205','1200',200);   
      insert   into   test_tree   values(9,'1207','1200',150);   
      commit;   select * from test_tree;
        
    select b.username,
           b.p_username,
           (select distinct sum(nvl(a.num, 0))
              from test_tree a
             start with a.username = b.username
            connect by a.p_username = prior a.username) sum
      from test_tree b
     start with username = '1000'
    connect by p_username = prior username;
      

  3.   

    谢谢2楼。感觉您的答案挺清晰的。不过也许我不该把问题贴在这个版,我用的是微软的ACCESS,刚才把您最后的SQL贴到ACCESS里,貌似它不认识。自己BS一下自己。如果您碰巧熟悉ACCESS,能否帮助翻译成微软的SQL,谢谢??
      

  4.   

    ACCESS没有这么好用的函数