日期:2014-05-18  浏览次数:20571 次

请教一个树形结构的sql语句怎么些写?求救了 !!!
现有一个表 tb
id price sum
-----------------------------------
12.34 null
12.34.1 2
12.34.2 5
12.35 null
12.35.1. null
12.35.1.1 null
12.35.1.1.1 1
12.35.1.1.2 2
12.35.1.2 1
12.35.2 null
12.35.2.1 1
12.35.2.2 2

现在要逐级的汇总price,请教该怎么写sql???
比如说12.35,就是由12.35.1和12.35.2加上来的,而12.35.1又是由12.35.1.1和12.35.1.2汇总而来的,12.35.1.1又是12.35.1.1.1和12.35.1.1.2加上的,应该最后12.35的price等于7,请教大侠们该怎么写啊???


------解决方案--------------------
SQL code
--测试数据
CREATE TABLE tb(ID char(3),PID char(3),Name nvarchar(10))
INSERT tb SELECT '001',NULL ,'山东省'
UNION ALL SELECT '002','001','烟台市'
UNION ALL SELECT '004','002','招远市'
UNION ALL SELECT '003','001','青岛市'
UNION ALL SELECT '005',NULL ,'四会市'
UNION ALL SELECT '006','005','清远市'
UNION ALL SELECT '007','006','小分市'
GO

--查询指定节点及其所有子节点的函数
CREATE FUNCTION f_Cid(@ID char(3))
RETURNS @t_Level TABLE(ID char(3),Level int)
AS
BEGIN
    DECLARE @Level int
    SET @Level=1
    INSERT @t_Level SELECT @ID,@Level
    WHILE @@ROWCOUNT>0
    BEGIN
        SET @Level=@Level+1
        INSERT @t_Level SELECT a.ID,@Level
        FROM tb a,@t_Level b
        WHERE a.PID=b.ID
            AND b.Level=@Level-1
    END
    RETURN
END
GO

--调用函数查询002及其所有子节点
SELECT a.*
FROM tb a,f_Cid('002') b
WHERE a.ID=b.ID
/*--结果
ID   PID  Name       
------ ------- ---------- 
002  001  烟台市
004  002  招远市
--*/

------解决方案--------------------
SQL code
update tb set sum = (select sum(price) from tb b where b.id like a.id+'%')
from tb a

------解决方案--------------------
SQL code
create table tb(id varchar(20), price int) 
insert into tb values('12.34'      ,      0 )
insert into tb values('12.34.1'    ,      2 )
insert into tb values('12.34.2'    ,      5 )
insert into tb values('12.35'      ,      0 )
insert into tb values('12.35.1'    ,      0 )
insert into tb values('12.35.1.1'  ,      0 )
insert into tb values('12.35.1.1.1',      1 )
insert into tb values('12.35.1.1.2',      2 )
insert into tb values('12.35.1.2'  ,      1 )
insert into tb values('12.35.2'    ,      0 )
insert into tb values('12.35.2.1'  ,      1 )
insert into tb values('12.35.2.2'  ,      2 )
go

select * , [sum] = (select sum(price) from tb where id like t.id + '%') from tb t

drop table tb

/*
id                   price       sum         
-------------------- ----------- ----------- 
12.34                0           7
12.34.1              2           2
12.34.2              5           5
12.35                0           7
12.35.1              0           4
12.35.1.1            0           3
12.35.1.1.1          1           1
12.35.1.1.2          2           2
12.35.1.2            1           1
12.35.2              0           3
12.35.2.1            1           1
12.35.2.2            2           2

(所影响的行数为 12 行)
*/

------解决方案--------------------
事先生名,我不要分 ,希望对你有帮助。

(转)
有关树的运用和存储过程
SQL code

--建立測試環境
Create Table department
(departmenid Int,
parentid Int)
Insert department Select 60, null
Union All Select 1, 0
Union All Select 2, 1
Union All Select 3, 1
Union All Select 4, 2
Union All Select 5, 2
Union All Select 6, 3
Union All Select 7, 3
Union All Select 8, 7
GO
--建立函數
Create Function F_GetParent(@departmenid Int)
Returns @Tree Table (departmenid Int, parentid Int)
As
Begin
Insert @Tree Select * From department Where departmenid = @departmenid
While @@Rowcount > 0
Insert @Tree Select A.* From department A Inner Join @Tree B On A.departmenid = B.parentid And A.departmenid Not In (Select departmenid From @Tree) Where A.parentid Is Not Null
Return
End
GO
--測試
Select departmenid From dbo.F_GetPar