I’ve been pondering this problem for a while and can’t find the solution (It might be simple.)
I have a table with two columns which shows which ID’s are connected, that is, belonging to the same person.
In this example there are only three individuals, but one of them has three unique IDs.
PID | EPID
---------+--------
10004835 | 10004835
10015375 | 10015375
10015375 | 10019859
10019859 | 10015375
10019859 | 10019859
10019859 | 10000000
10000000 | 10019859
10020104 | 10020104
What I want to do is simply to add a column to this table which gives each unique individual a unique code. That is something like
PID | EPID | NPID
---------+----------+-----
10004835 | 10004835 | 1
10015375 | 10015375 | 2
10015375 | 10019859 | 2
10019859 | 10015375 | 2
10019859 | 10019859 | 2
10019859 | 10000000 | 2
10000000 | 10019859 | 2
10020104 | 10020104 | 3
Ps. I’m using sqlite3 so no recursion in answers please.
Edit: Unless I can find a solution which works for SQLITE3 I will have to use MYSQL instead. In that case, does anyone know a solution which includes recursion?
if you have an upper limit on how long any connected IDs chain can be, you can self-join the table that many times and get the least (or the greatest) of all the ids: