Select pair of rows that obey a rule

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Lior Ainey
    New Member
    • Oct 2011
    • 3

    Select pair of rows that obey a rule

    Hi,
    I have a big table (1M rows) with the following columns:
    source, dest, distance.
    Each row defines a link (from A to B).

    I need to find the distances between a pair using anoter node.
    An example:
    If want to find the distance between A and B,
    If I find a node x and have:
    x -> A
    x -> B
    I can add these distances and have the distance beetween A and B.
    My question:
    How can I find all the nodes (such as x) and get their distances to (A and B)?
    My purpose is to select the min value of distance.

    P.s: A and B are just one connection (I need to do it for 100K connections).
    Thanks !
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    You must mean from A to x and then from x to B. Otherwise, it doesn't make much sense. If we are talking at most one intermediate node, you can just join the table to itself, filtering for you start and end node, and doing an aggregate query. However, if you are talking about n-number of nodes where n is not known, your table structure does not support that type of query. You would have to redesign your table to include a tree data structure of some sort.

    Comment

    • Lior Ainey
      New Member
      • Oct 2011
      • 3

      #3
      Thanks Rabbit.
      There is no mistake with the directions:
      For measuring A->B, I'm looking for the node (X) that gives a minimum value for dist(X->A) + dist(X->B).
      I have several topologies I need to check (2 nodes...) but I first want to solve the basic one.
      Can you help me with the query your suggested (using JOIN)?
      Thanks

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        My point is that solving for the specific form is a waste of time if your end goal is to solve for the generic form. Yes, we can get a solution to solve for the 1-intermediary node problem, but the solution for n-intermediary node is completely different.

        Comment

        • Lior Ainey
          New Member
          • Oct 2011
          • 3

          #5
          I'm not interested in a solution for n-intermediary node, but only in several topologies (for 1 and 2 intermediary nodes).
          I want to try with a simple topology and derive the others respectively.
          I'll be happy to get help with the query.
          Thanks

          Comment

          • Rabbit
            Recognized Expert MVP
            • Jan 2007
            • 12517

            #6
            Basically, join the table to itself and filter for the end points.
            Code:
            SELECT s.NodeB AS starting,
               e.NodeB AS ending,
               s.Distance + e.Distance AS totalDistance
            FROM tableName AS s
            INNER JOIN tableName AS e
            ON s.NodeA = e.NodeA
               AND s.NodeB != e.NodeB
            WHERE s.NodeB = 'A' AND e.NodeB = 'B'

            Comment

            Working...