Forum Stats

  • 3,734,267 Users
  • 2,246,934 Discussions
  • 7,857,216 Comments

Discussions

Link Rows using other column in the same row

2943443
2943443 Member Posts: 3
edited May 5, 2015 7:27AM in SQL

Link all rows based on values:

All - I have a requirement like this:

My Table has 2 columns - Col1, Col2

select * from tab1;

Col1, Col2

A1     C1

A1     C2

A2     C2

A2     C3

A3     C3

A4     C4

A5     C2

Now, I have to create a link like this (In the above example, A1, A2, A3 and A5 are related because of Col2 -

A1 is related to C1 and C2 - A2 is related to A1 because they both have C2 and hence related to A1 - A3 is related to A2 because of C3 (and hence related to A1) and A5 is related to A2 because of C2 (and hence A1 and A3)

A4 is not related to anything and hence independent

L1     A1

L1     A2

L1     A3

L1     A5

L2     A4

How do I do it using analytic function or any other means - I achieved the same using cursors - but it is taking too long and messy. Any help would be appreciated.

Thanks

Ram

Sign In or Register to comment.