一个树形结构的数据
比如数据库中有三个字段 一个是value 一个是sort,这个是判断处于第几级节点,一个是ID
id value   sort
1    1     1001
2    2     1001001
3    3     1001001001
4    4     1001001002
5    5     1001002就是这样,如果我选中的ID是1的节点,那我就相加他下面子节点的value,但是却不能重复相加,比如ID2有值,就不能相加ID 为2下面的ID3,4子节点,因为ID2的值是ID3,4的值的和,
有没有方法是自动相加最低级节点的和,然后付值给他上一级节点,一直到付值给选中的最高节点为止?  

解决方案 »

  1.   

    value有点问题,我修改下
    一个树形结构的数据
    比如数据库中有三个字段 一个是value 一个是sort,这个是判断处于第几级节点,一个是ID
    id value sort
    1 12 1001
    2 7 1001001
    3 3 1001001001
    4 4 1001001002
    5 5 1001002就是这样,如果我选中的ID是1的节点,那我就相加他下面子节点的value,但是却不能重复相加,比如ID2有值,就不能相加ID 为2下面的ID3,4子节点,因为ID2的值是ID3,4的值的和,
    有没有方法是自动相加最低级节点的和,然后付值给他上一级节点,一直到付值给选中的最高节点为止?
      

  2.   


    ID为1的value是ID2,5value的相加,ID为2的value是ID 3,4 value的相加,我就是想要选中一个节点,然后自动相加他下面节点的value,但不能重复相加,明白我的意思吗?
      

  3.   

    典型的BOM结构 查查精华帖子 没环境 没办法帮你写
      

  4.   


    USE City;
    GO
    SET NOCOUNT ON
    IF OBJECT_ID(N'A',N'U')IS NOT NULL DROP TABLE A
    GO
    IF OBJECT_ID('dbo.FF','FN') IS NOT NULL DROP FUNCTION dbo.FF
    GO
    CREATE TABLE A--创建测试数据表
    (
    id int,
    value int,
    sort nvarchar(100)
    )
    INSERT INTO A--插入测试数据
    select 1, 1, '1001' union all
    select 2, 2, '1001001' union all
    select 3, 3, '1001001001' union all
    select 4, 4, '1001001002' union all
    select 5, 5, '1001002'
    go
    create function dbo.FF(@select_id int)
    returns int
    begin
    declare @sum int;
    with cte as
    (
    select id,value,sort,0 as myvalue from A where id=@select_id
    union all
    select a1.id,a1.value,a1.sort,
    myvalue=isnull((
    select value from A a2 where Not exists(select * from A a3 where a3.sort like a2.sort+'___') and  a1.sort=a2.sort
    ),0) from cte inner join A a1 on a1.sort like cte.sort+'___'
    )
    select @sum=SUM(myvalue) from cte
    return @sum
    end
    go
    declare @select_id int
    set @select_id=1
    select dbo.FF(@select_id)
    /*
    -----------
    12
    */
      

  5.   

    如果选中节点本身的值也要加入结果的话,那就是USE City;
    GO
    SET NOCOUNT ON
    IF OBJECT_ID(N'A',N'U')IS NOT NULL DROP TABLE A
    GO
    IF OBJECT_ID('dbo.FF','FN') IS NOT NULL DROP FUNCTION dbo.FF
    GO
    CREATE TABLE A--创建测试数据表
    (
    id int,
    value int,
    sort nvarchar(100)
    )
    INSERT INTO A--插入测试数据
    select 1, 1, '1001' union all
    select 2, 2, '1001001' union all
    select 3, 3, '1001001001' union all
    select 4, 4, '1001001002' union all
    select 5, 5, '1001002'
    go
    create function dbo.FF(@select_id int)
    returns int
    begin
    declare @sum int;
    with cte as
    (
    select id,value,sort,value as myvalue from A where id=@select_id
    union all
    select a1.id,a1.value,a1.sort,
    myvalue=isnull((
    select value from A a2 where Not exists(select * from A a3 where a3.sort like a2.sort+'___') and  a1.sort=a2.sort
    ),0) from cte inner join A a1 on a1.sort like cte.sort+'___'
    )
    select @sum=SUM(myvalue) from cte
    return @sum
    end
    go
    declare @select_id int
    set @select_id=1
    select dbo.FF(@select_id)
    /*
    -----------
    13
    */