Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all!
I have a problem in SQL Server must everyone help.
My database has the form (ProID, SubProid, value)
example:
A---B---0.2
B---C---0.1
C---D---0.5
A---D---0.6
B---D---0.3


a given SubproId can have multiple paths to reach from a given proID.
How do I calculate the sum weight of every the path from the 2 points given
example(sum of every weight between A--D=0.6+0.8+0.5=1.9)?
Posted
Updated 19-Oct-12 23:06pm
v2
Comments
Zoltán Zörgő 20-Oct-12 5:15am    
Do you really need this in t-sql? Why not on higher level of the application?
Please give a more complex example.
Sandeep Mewara 20-Oct-12 5:46am    
What have you tried so far? Where are you stuck?

You should research the following

shortest path problem/algorithm

The presentation of the problem suggests to me it is homework. It isn't simple to solve and requires a non-trivial understanding of algorithms and SQL to solve it on ones own (I certainly couldn't solve it without researching the algorithm side.)
 
Share this answer
 
I don't see this as "shortest path" problem. More something that we could call "all paths" - but I am not sure. Ok, T-SQL is not the language for graph algorithms, but not impossible to handle either.
This is a good article about some general graph-related problem solutions in t-sql: http://hansolav.net/sql/graphs.html[^]. Here is an article about the "all paths" problem: http://www.technical-recipes.com/2011/a-recursive-algorithm-to-find-all-paths-between-two-given-nodes/[^]. This is not t-sql, but you can understand the concept.
You could use these approaches as samples and starting point. As you can see, it is not the simplest problem. I am pretty sure you will need to use fancy things like temporary tables and cursors.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900