问题
I have a query that returns a set of rows with a combination of the letters A, E, I and L. That combination are Portuguese mneumonics for Alterar, Excluir, Incluir, and Listar. The column value can vary between A, AE, AEL, AELI... L, LI, LIA, LIAE.
In the example illustrated below I would like to be able to get a unique string with all repeated letters removed. So the result would be AELI, a merge of all rows.
My first attempt was using the combination of the functions array_to_string and array_agg.
select array_to_string(array_agg(colmn), '') from mytable;
then I used the regexp_matches function for matching only one character of the same letter, but with no success, because returned AELA.
select regexp_matches('AELAEILI', '[?AEIL][?AEIL][?AEIL][?AEIL]');
Here the examples explanated above.
Note
That problem explanated here is a piece of a great sql instruction. This sqlfiddle is my effort of simulating my real problem.
回答1:
I think this can be achieved by using the regexp_split_to_table postgresql function.
From the docs:
Split string using a POSIX regular expression as the delimiter. See Section 9.7.3 for more information.
So, if have the following data:
CREATE TABLE example_table (col TEXT);
INSERT INTO example_table(col) VALUES
('AEL'),
('A'),
('E'),
('I'),
('L'),
('LI');
The result of the query SELECT regexp_split_to_table(col, '') as col
will be
col
A
E
L
A
E
I
L
L
I
After managing to split the words like that, filtering the duplicates is pretty simple by adding the DISTINCT statement:
SELECT DISTINCT(regexp_split_to_table(col, '')) as col
FROM example_table
The result of this query is going to be
col
A
E
I
L
At the end we can convert the result rows to a single string with the following code:
SELECT array_to_string(array_agg(col),'') FROM (
SELECT DISTINCT(regexp_split_to_table(col, '')) as col
FROM example_table
) temp_table;
来源:https://stackoverflow.com/questions/50867514/removing-repeated-letters-in-a-set-of-rows-on-postgresql-8-3