Hi All I urgently need help with this SQL query.
I have one SQL table, (I didn't build this so don't ask me why it's structured like this) but I need to urgently extract data and I'm stuck on this step.
So basically there is one Table in the database it has 3 columns.
ID, NextID, comments.
Comments column isn't used for anything.
This table shows the linkage that NextID is next in sequence after this ID.
What SQL will allow me to create a new table that maps all the IDs from the same "Group" in the sequence.
e.g. Here is some sample data:
ID, NextID, comments.
1, 5,
5, 11
6, 8
11, 26
22, 37
so 1, 5, 11, 26 are from the same family (same sequence) because they follow each other.
because 1 goes to 5, then 5 goes to 11, 11 goes to 26.
6, 8 are one Group.
22, 37 are one Group.
the sequence could be up to 10 links I guess or longer maybe.
The result table I wish to have is:
GroupID, ID
A,1
A,5
A,11
A,26
B,6
B,8
C,22
C, 37
ideally those that don't have links, just by themselves also get a groupID . Like ID 2, 3, 4, 7, 9, 10 etc are just single and nothing follows them in the sequence.
Thanks in advance for your help. I'm using Microsoft SQL Studio. SQL Server 13.0.5492.2