其实是一个二叉数     1下面有两个子2,3 ,2下面有两个子4,5
我想查出1的两个子中最小的cunliang,2的为400,3的是100,所以是100,然后2的shuliang-100,3的shuliang也减去100 2下面有两个子4,5,找出4和5中最小的cunliang=300, 然后4的cunliang-300  5 的cunliang-300
不知道我说明白了吗

解决方案 »

  1.   


    UPDATE LI SET CUNLIANG = CUNLIANG - LB.CUNLIANG 
    FROM LI,
    (
    SELECT PARENT,MIN(CUNLIANG) AS CUNLIANG 
    FROM  
    (
    SELECT COUNT(1) AS RMARK,PARENT 
    FROM LI 
    GROUP BY PARENT
    HAVING(COUNT(1) > 2)) LO   
    GROUP BY PARENT) LB  WHERE LI.PARENT  = LB.PARENT  
      

  2.   

    假定你说的子是指的直接孩子,而不是孙子或更小的后辈;
    再假定你只需要选择兄弟(同父)个数大于2的行,其余行忽略;select ID,Parent,CunLiang-A.CunLiang
    from T join
      (
       select Parent,min(CunLiang)
       from T
       group by Parent
       having count(*)>1
      ) A
    on T.Parent=A.Parent
      

  3.   

    好吧,没看清楚,借鉴下5楼朋友的答案:Update T
    set CunLiang = CunLiang-A.CunLiang 
    from T join 
      ( 
      select Parent,min(CunLiang) as CunLiang
      from T 
      group by Parent 
      having count(*)>2 
      ) A 
    on T.Parent=A.Parent 
      

  4.   

    Update T 
    set CunLiang = CunLiang-A.CunLiang 
    from T join 
      ( 
      select Parent,min(CunLiang) as CunLiang 
      from T 
      group by Parent 
      having count(*)=2 
      ) A 
    on T.Parent=A.Parent 答案了