Answer the question
In order to leave comments, you need to log in
How to calculate full name in columns with wrong order?
Given:
id | col1 | col2 | col3
--------------------------------------
1 | Иванов | Иван | Иванович
2 | Иван | Иванович | Иванов
3 | Петров | Сергей | Алексеевич
4 | Петров | Сергей | Алексеевич
5 | Сергей | Алексеевич| Петров
Answer the question
In order to leave comments, you need to log in
Implementation example for MySQL 8+:
WITH
cte1 AS ( SELECT id, col1 val FROM test UNION ALL
SELECT id, col2 FROM test UNION ALL
SELECT id, col3 FROM test ),
cte2 AS ( SELECT id, GROUP_CONCAT(val ORDER BY val) FIO
FROM cte1
GROUP BY id )
SELECT test.id, test.col1, test.col2, test.col3, GROUP_CONCAT(cte2_2.id) ids
FROM test
JOIN cte2 cte2_1 USING (id)
JOIN cte2 cte2_2 USING (FIO)
GROUP BY test.id, test.col1, test.col2, test.col3
id col1 col2 col3 ids
1 Иванов Иван Иванович 1,2
2 Иван Иванович Иванов 1,2
3 Петров Сергей Алексеевич 3,4,5
4 Петров Сергей Алексеевич 3,4,5
5 Сергей Алексеевич Петров 3,4,5
DEMOwill be executed on MS SQL Server, I can't tell the version.
You can only hardly request. More like code.
Analyze the endings of words, make lists of endings characteristic of surnames, names, patronymics. As well as exclusion lists. And all the same, the result will need to be checked with the eyes and corrected in some places.
Data cleaning is such an expensive and troublesome business. And it is desirable to conduct it before entering the base.
since the goal is to find the same people with a confused order
, find records in which col1 matches col2 then col3, then col2 with col3, you will get a list of pairs in which this is confused, carefully, degenerate cases (for example, initials are written in the full name) or for example, oriental names (there are very complex composite names that are written down by someone in what they are), these moments are best processed with separate logic
select a.*,b.*
from table a inner join table b on
a.col1=b.col2
-- a.col1=b.col3
-- a.col2=b.col3
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question