A D : 80 * 90 * 50 + 60 =..存在路经: A D 60 -- 60A B 80 B C 90 C D 50 --80*90*50A C 50 C D 50 --?为什么这个路经没有?
是不是单向的?A C : 50 + 80*90 = .. 存在路经: A C 50 -- 50A B 80 B C 90 --80*90A D 60 C D 50 --这条有反向的算不算?
当单向图计算--建立源表 declare @b table( [From] varchar(10), [To] varchar(10), [Percent] numeric(18,2) )--源表数据 insert @b select 'A', 'B', 80 union all select 'A', 'C', 50 union all select 'A', 'D', 60 union all select 'B', 'C', 90 union all select 'C', 'D', 50 --计算结果表 declare @t table( [From] varchar(10), [To] varchar(10), [Percent] numeric(18,2), [Path] varchar(1000) )--搜索所有路经 insert @t select *,[From]+','+[To] from @bwhile exists ( select 1 from @t t,@b b where t.[To]=b.[From] and not exists ( select 1 from @t where [Path]=t.[Path]+','+b.[To] ) ) insert @t select t.[From],b.[To],t.[Percent]*b.[Percent],t.[Path]+','+b.[To] from @t t,@b b where t.[To]=b.[From] and not exists ( select 1 from @t where [Path]=t.[Path]+','+b.[To] )--测试数据 declare @From varchar(10) declare @To varchar(10)set @From='A' set @To='C'--看路经 select * from @t where [From]=@From and [To]=@To--最终结果 select sum([Percent]) as [Percent] from @t where [From]=@From and [To]=@To
A D 60 -- 60A B 80
B C 90
C D 50 --80*90*50A C 50
C D 50 --?为什么这个路经没有?
存在路经:
A C 50 -- 50A B 80
B C 90 --80*90A D 60
C D 50 --这条有反向的算不算?
declare @b table(
[From] varchar(10),
[To] varchar(10),
[Percent] numeric(18,2)
)--源表数据
insert @b select
'A', 'B', 80
union all select
'A', 'C', 50
union all select
'A', 'D', 60
union all select
'B', 'C', 90
union all select
'C', 'D', 50
--计算结果表
declare @t table(
[From] varchar(10),
[To] varchar(10),
[Percent] numeric(18,2),
[Path] varchar(1000)
)--搜索所有路经
insert @t
select *,[From]+','+[To] from @bwhile exists (
select 1 from @t t,@b b
where t.[To]=b.[From]
and not exists (
select 1 from @t where [Path]=t.[Path]+','+b.[To]
)
)
insert @t
select t.[From],b.[To],t.[Percent]*b.[Percent],t.[Path]+','+b.[To] from @t t,@b b
where t.[To]=b.[From]
and not exists (
select 1 from @t where [Path]=t.[Path]+','+b.[To]
)--测试数据
declare @From varchar(10)
declare @To varchar(10)set @From='A'
set @To='C'--看路经
select * from @t
where [From]=@From
and [To]=@To--最终结果
select sum([Percent]) as [Percent]
from @t
where [From]=@From
and [To]=@To
fcuandy(现在流行散分) ( ) 信誉:100 2006-08-16 09:33:00 得分: 0
AC不是50吗?为什么还要算?
还是即有 AC, 还有 A-B, B-C 所以再多算出一个AC?
这样:
A B : 80%
A C : 50% + 80*90% = 122%
A D : 80% * 90% * 50% + 60% =96%
您好,我们是“2006中国杰出数据库工程师评选”活动组委会。
您的帖子已经被我们转载到本次评选官方网站的“专家在线答疑”区。
http://www.bestdba.cn/match_discussion.aspx在那里,进入本次评选终选的30位数据库工程师将与您展开积极的互动。他们会为您的问题提供满意的答案,此外,您还可以在“专家在线答疑”区提出新的问题并参与讨论。您的帖子位于:
http://www.bestdba.cn/match_discussion3.aspx?pointid=552&pointid2=1&pointid3=5&pcount=stc
非常感谢您对本次活动的支持!
把数据加载出来,然后根据数据构造一个图,然后再遍历这个图,随意计算两个点之间的路径权值如果数据量过大,那么适当采取一些规则来做到小范围的查找。
而进入死循环。
进群需有.NET开发经验
欢迎广大。NET开发者踊跃加入
如果是这样,那用函数做简单的ASC码操作就很容易实现了不过建议你这种算法问题,如果考虑效率,按道理来说,应该是取到C里然后计算,如果非要用SQL计算,最好用上临时表,把数据放内存里计算。