http://community.csdn.net/Expert/TopicView3.asp?id=3996740
http://community.csdn.net/Expert/TopicView3.asp?id=3995429
先前开的两贴,都没有解决,谁搞定这问题 ,悉数奉上。更直观点的表
表结构如下:
ID -- Parent -- Item1 -- Item2
1 ---- 0 ----- x=4 ----- x=8
-- 3 ---- 1 ----- x=3 ----- x=4
---- 5 ---- 3 ----- 1 ------- 2
---- 6 ---- 3 ----- 2 ------- 2
-- 4 ---- 1 ----- 1 ------- 4
2 ---- 0 ----- x=N ----- x=M
7 ---- 0 ----- x=X ----- x=Y
http://community.csdn.net/Expert/TopicView3.asp?id=3995429
先前开的两贴,都没有解决,谁搞定这问题 ,悉数奉上。更直观点的表
表结构如下:
ID -- Parent -- Item1 -- Item2
1 ---- 0 ----- x=4 ----- x=8
-- 3 ---- 1 ----- x=3 ----- x=4
---- 5 ---- 3 ----- 1 ------- 2
---- 6 ---- 3 ----- 2 ------- 2
-- 4 ---- 1 ----- 1 ------- 4
2 ---- 0 ----- x=N ----- x=M
7 ---- 0 ----- x=X ----- x=Y
http://community.csdn.net/Expert/TopicView3.asp?id=3996740
http://community.csdn.net/Expert/TopicView3.asp?id=3995429
先前开的两贴,都没有解决,谁搞定这问题 ,悉数奉上三贴的分数。更直观点的表表结构如下:
ID -- Parent -- Item1 -- Item2
1 ---- root ----- x=4 ----- x=15
-- 3 ---- 1 ----- x=3 ----- x=11
---- 5 ---- 3 ----- 1 ------- 5
---- 6 ---- 3 ----- 2 ------- 6
-- 4 ---- 1 ----- 1 ------- 4
2 ---- root ----- x=N ----- x=M
root---- 0 ----- x=4+N ----- x=15+M
ID -- Parent -- Item1 -- Item2
1 ---- root ----- ? ----- ?
-- 3 ---- 1 ----- ? ----- ?
---- 5 ---- 3 ----- 1 ------- 5
---- 6 ---- 3 ----- 2 ------- 6
-- 4 ---- 1 ----- 1 ------- 4
2 ---- root ----- ? ----- ?
root---- 0 ----- ? ----- ?要实现的功能:求出?的值。ID=2 也是父结点 ,与ID=1的结点类似。ID -- Parent -- Item1 -- Item2
1 ---- root ----- 4 ----- 15
-- 3 ---- 1 ----- 3 ----- 11
---- 5 ---- 3 ----- 1 ------- 5
---- 6 ---- 3 ----- 2 ------- 6
-- 4 ---- 1 ----- 1 ------- 4
2 ---- root ----- ? ----- ?
root---- 0 ----- 4+? ----- 15+?
root 有两个子结点 id=1 和 2
1 有两个子结点 id=3 和 4
3 有两个子结点 id=5 和 6
insert tb select 1,0 ,null,null
union all select 3,1 ,null,null
union all select 5,3 ,1 ,5
union all select 6,3 ,2 ,6
union all select 4,1 ,1 ,4
union all select 2,0 ,2222,4444 --2没有子结点,应该直接有值
union all select 0,null,null,null
go--更新处理的存储过程
create proc p_calc
as
update a set Item1=null,Item2=null
from tb a
where exists(
select * from tb where Parent=a.ID)
while @@rowcount>0
update a set Item1=b.Item1,Item2=b.Item2
from tb a,(
select Parent,Item1=sum(Item1),Item2=sum(Item2)
from tb a
group by Parent
having sum(case when Item1 is null then 1 else 0 end)
+sum(case when Item2 is null then 1 else 0 end)=0
)b where a.ID=b.Parent
and(a.Item1 is null or a.Item2 is null)
go--调用
exec p_calc
select * from tb
go--删除测试
drop table tb
drop proc p_calc/*--结果ID Parent Item1 Item2
----------- ----------- ----------- -----------
1 0 4 15
3 1 3 11
5 3 1 5
6 3 2 6
4 1 1 4
2 0 2222 4444
0 NULL 2226 4459(所影响的行数为 7 行)
--*/
insert tb select 1,'root',null,null
union all select 3,1 ,null,null
union all select 5,3 ,1 ,5
union all select 6,3 ,2 ,6
union all select 4,1 ,1 ,4
union all select 2,'root',2222,4444 --2没有子结点,应该直接有值
union all select 'root',0,null,null
go--更新处理的存储过程
create proc p_calc
as
update a set Item1=null,Item2=null
from tb a
where exists(
select * from tb where Parent=a.ID)
while @@rowcount>0
update a set Item1=b.Item1,Item2=b.Item2
from tb a,(
select Parent,Item1=sum(Item1),Item2=sum(Item2)
from tb a
group by Parent
having sum(case when Item1 is null then 1 else 0 end)
+sum(case when Item2 is null then 1 else 0 end)=0
)b where a.ID=b.Parent
and(a.Item1 is null or a.Item2 is null)
go--调用
exec p_calc
select * from tb
go--删除测试
drop table tb
drop proc p_calc/*--结果ID Parent Item1 Item2
---------- ---------- ----------- -----------
1 root 4 15
3 1 3 11
5 3 1 5
6 3 2 6
4 1 1 4
2 root 2222 4444
root 0 2226 4459(所影响的行数为 7 行)
--*/
http://www.ccw.com.cn/cio/research/program/htm2003/20030111_145X6.asp我的表达可能令人费解一些,谢谢你们的耐心指导。
insert tb select 1,'root',null,null
union all select 3,1 ,null,null
union all select 5,3 ,1 ,5
union all select 6,3 ,2 ,6
union all select 4,1 ,1 ,4
union all select 2,'root',null,null
union all select 'root',0,null,null
go--更新处理的存储过程
create proc p_calc
as
update a set
Item1=case
when exists(select * from tb where Parent=a.ID) then null
else isnull(Item1,0) end,
Item2=case
when exists(select * from tb where Parent=a.ID) then null
else isnull(Item2,0) end
from tb awhile @@rowcount>0
update a set Item1=b.Item1,Item2=b.Item2
from tb a,(
select Parent,Item1=sum(Item1),Item2=sum(Item2)
from tb a
group by Parent
having sum(case when Item1 is null then 1 else 0 end)
+sum(case when Item2 is null then 1 else 0 end)=0
)b where a.ID=b.Parent
and(a.Item1 is null or a.Item2 is null)
go--调用
exec p_calc
select * from tb
go--删除测试
drop table tb
drop proc p_calc/*--结果ID Parent Item1 Item2
---------- ---------- ----------- -----------
1 root 4 15
3 1 3 11
5 3 1 5
6 3 2 6
4 1 1 4
2 root 0 0
root 0 4 15(所影响的行数为 7 行)
--*/
麻烦 达人去我开的另外两贴 Mark 一下。
我给你结贴。十分的感谢。
另外两贴已结,这个贴子马上就结。
顺便请教一下,推荐本 SQL 的经典教程。我也认认真真的学习一下。
因为我只知道这样实现了,但不知道您的思路,以及您为什么这些语句用的这么炉火纯青。PF PF
http://www.phoenixtv.com/phoenixtv/72622743014604800/20050511/547996.shtml