postgresql - Create unique identifier for more persons that are the same (SQL) -
i have 2 tables:
|-------------| |-------------| | person | | alias | |-------------| |-------------| | person_id | | person_id_1 | | person_name | | person_id_2 | |-------------| |-------------| the alias table tells me if 2 persons in fact same person. if case, want have unique id these persons.
e. g.: |-------------------------| | person | |-----------|-------------| | person_id | person_name | |-----------|-------------| | 1 | michail | |-----------|-------------| | 2 | michail | |-----------|-------------| | 3 | petja | |-----------|-------------|
|---------------------------| | alias | |-------------|-------------| | person_id_1 | person_id_2 | |-------------|-------------| | 1 | 2 | |-------------|-------------|
now want create view, query, you name it, lists me person_ids , unique identifier these ids.
|-----------------------| | unique id view | |-----------------------| | unique_id | person_id | |-----------------------| | xe3rf | 1 | | xe3rf | 2 | | y23ij | 3 | |-----------------------| can me unique id view? totally clueless atm :-(
thanks inb4,
michail
i think have value can use uniqe id. have force person_id_1 unique on same person. make sure don't have tree-like references:
1 -> 2 2 -> 3 2 -> 4 these tree-like connections difficult handle in sql.
lets rename person_id_1 original_id , person_id_2 secondary_id. if find new connection, "5 duplicate 3", query table:
- is 3 exists on original_id column? if yes, insert new connection.
- is 3 exists on secondary_id column? if yes, query row's original_id, , insert new connection that.
this way can avoid chains, , query want simple as
select * alias as @a_horse_with_no_name suggested, can simply use query recursive with statement too:
with recursive conn(person_id_1, person_id_2) ( select person_id_1, person_id_2 alias union select alias.person_id_1, conn.person_id_2 alias, conn alias.person_id_2 = conn.person_id_1 ), leaf_nodes ( select distinct person_id_2 alias ) select * conn conn.person_id_1 not in (select person_id_2 leaf_nodes) order person_id_1; wiki
Comments
Post a Comment