build relationship between columns

  • Hi,

    I have a table stores relationship between columns

    ColA ColB

    1 2

    2 1

    1 3

    3 1

    ....

    4 5

    5 6

    4 7

    7 4

    Does someone know how to add the missing relationships like 2-3,3-2 and 5-4,4-6,6-4.....without using cursor?

    Thanks

  • Hi,

    what kind of relationship you have in between A & B?

    Thanks -- Vj

    http://dotnetvj.blogspot.com

  • Are you looking for an Islands and Gaps solution?

    This is from Itzik Ben-Gan's Inside TSQL Querying and identifies missing values

    SELECT cur+1 AS start_range, nxt-1 AS end_range

    FROM (SELECT col1 AS cur,

    (SELECT MIN(col1) FROM dbo.T1 AS B

    WHERE B.col1 > a.col1) AS nxt

    FROM dbo.T1 AS A) AS D

    WHERE nxt-cur > 1;

    You'd have to find a way to run this for both sets of numbers to identify the missing values. Logically a bit of a twister, but a very simple query.

    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
    - Theodore Roosevelt

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply